dotnet/efcore · error · InvalidOperationException
The default value has not been specified for the column '{ta
Error message
The default value has not been specified for the column '{table}.{column}'. Specify a value before using Entity Framework to create the database schema. What it means
Thrown by MigrationsModelDiffer.Initialize when a column's `DefaultValue` equals `DBNull.Value`, meaning the model says a default value should exist but none was supplied. This breaks `EnsureCreated`/migration scaffolding because the column would be created with no default to write for non-nullable rows.
Source
Thrown at src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:1226
};
operation.AddAnnotations(MigrationsAnnotationProvider.ForRemove(source));
diffContext.AddDrop(source, operation);
yield return operation;
}
private void Initialize(
ColumnOperation columnOperation,
IColumn column,
RelationalTypeMapping typeMapping,
bool isNullable,
IEnumerable<IAnnotation> migrationsAnnotations,
bool inline = false)
{
if (column.DefaultValue == DBNull.Value)
{
throw new InvalidOperationException(
RelationalStrings.DefaultValueUnspecified(
column.Table.SchemaQualifiedName,
column.Name));
}
if (column.DefaultValueSql?.Length == 0)
{
throw new InvalidOperationException(
RelationalStrings.DefaultValueSqlUnspecified(
column.Table.SchemaQualifiedName,
column.Name));
}
if (column.ComputedColumnSql?.Length == 0)
{
throw new InvalidOperationException(
RelationalStrings.ComputedColumnSqlUnspecified(
column.Name,View on GitHub (pinned to dbf9771522)
Solutions
- Provide an explicit default value: `.HasDefaultValue(0)` / `.HasDefaultValueSql("0")`.
- If the column should be nullable, configure `.IsRequired(false)` so no default is needed.
- If a database-generated default exists, use `.HasDefaultValueSql("...")` to point at it.
- Remove the stray `.HasDefaultValue()` call if no default is intended.
Example fix
// before modelBuilder.Entity<Foo>().Property(x => x.Count).HasDefaultValue(); // after modelBuilder.Entity<Foo>().Property(x => x.Count).HasDefaultValue(0);
Defensive patterns
Strategy: validation
Validate before calling
// In OnModelCreating, ensure every HasDefaultValue() column has a real value or is nullable.
foreach (var prop in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetProperties()))
{
if (!prop.IsNullable
&& prop.GetDefaultValue() == DBNull.Value
&& string.IsNullOrEmpty(prop.GetDefaultValueSql())
&& prop.GetComputedColumnSql() is null)
{
// decide: set a value, a DefaultValueSql, or make the property nullable.
}
} Prevention
- Always pass an explicit value or SQL to `.HasDefaultValue`/`.HasDefaultValueSql`.
- Prefer `.HasDefaultValueSql("...")` when the default is database-side.
- Make columns nullable if no default is intended.
When it happens
Trigger: Configuring a column with `.HasDefaultValue()` (no argument) but no value/database-side default, then calling `EnsureCreated`, `Migrate`, or scaffolding migrations. Also reached when a non-nullable column maps to a store column that needs an explicit default that was never provided.
Common situations: Calling `.HasDefaultValue()` expecting it to infer a value; mixing computed/default annotations manually; building column operations in custom differ code; provider/version upgrade that changed default-value semantics.
Related errors
- The default value SQL has not been specified for the column
- The computed column SQL has not been specified for the colum
- A seed entity for entity type '{entityType}' has the same ke
- A seed entity for entity type '{entityType}' has the same ke
- A seed entity for entity type '{entityType}' has the same ke
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/d35c6fc69d0e7755.
Report an issue: GitHub.