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

  1. Provide an explicit default value: `.HasDefaultValue(0)` / `.HasDefaultValueSql("0")`.
  2. If the column should be nullable, configure `.IsRequired(false)` so no default is needed.
  3. If a database-generated default exists, use `.HasDefaultValueSql("...")` to point at it.
  4. 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

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


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/d35c6fc69d0e7755. Report an issue: GitHub.