dotnet/efcore · error · InvalidOperationException

The default value has not been specified for the column

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

When generating column DDL, EF requires a concrete default if a default was requested. Initialize throws when column.DefaultValue equals DBNull.Value, which marks an explicitly-requested-but-unspecified default. The column was flagged as having a default but no value was supplied.

Solutions

  1. Supply an explicit value to HasDefaultValue(value) or use HasDefaultValueSql(sql) / HasComputedColumnSql(sql).
  2. Remove the HasDefaultValue() call entirely if no default is intended.
  3. If a value converter is involved, ensure the provider-side default is set.
  4. Inspect the column mapping in the migration to see which property produced the DBNull default.

Example fix

// before
builder.Entity<Order>()
    .Property(o => o.Status)
    .HasDefaultValue(); // throws on migration

// after
builder.Entity<Order>()
    .Property(o => o.Status)
    .HasDefaultValue("Pending");
Defensive patterns

Strategy: validation

Validate before calling

// In model configuration, never call HasDefaultValue() without a value
// unless a value-converter/SQL default is intended elsewhere.
// Guard:
if (defaultValueRequested && defaultValue is DBNull or null)
{
    throw new InvalidOperationException("Supply a value or remove HasDefaultValue().");
}

Type guard

static bool HasValidDefault(object? defaultValue, string? defaultValueSql)
    => defaultValue is not DBNull || !string.IsNullOrEmpty(defaultValueSql);

Prevention

When it happens

Trigger: A column whose DefaultValue is set to DBNull.Value (rather than left null) during migration diffing; usually a property configured with HasDefaultValue() but no argument, or a value-converter scenario that nulls the default.

Common situations: Calling .HasDefaultValue() with no value on a non-nullable property; scaffolding that emits an empty default; migrations referencing a property whose default was never set; conventions that mark DefaultValue as requested without supplying it.

Related errors


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

Appendix: 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 3a2006ef56)