dotnet/efcore · error · InvalidOperationException

The default value SQL has not been specified for the column

Error message

The default value SQL has not been specified for the column '{table}.{column}'. Specify the SQL before using Entity Framework to create the database schema.

What it means

A column flagged as having a default-value SQL expression must carry a non-empty expression. Initialize throws when DefaultValueSql is the empty string, because the schema-generation pipeline cannot emit DEFAULT '' (empty) for a column that claims a default.

Solutions

  1. Pass a real SQL expression to HasDefaultValueSql("GETUTCDATE()").
  2. If no default SQL is intended, remove the call rather than passing empty.
  3. Coerce empty strings to null before assigning DefaultValueSql.
  4. Audit migration scaffolding output for empty default-SQL fragments.

Example fix

// before
builder.Entity<Order>()
    .Property(o => o.CreatedAt)
    .HasDefaultValueSql(""); // throws

// after
builder.Entity<Order>()
    .Property(o => o.CreatedAt)
    .HasDefaultValueSql("GETUTCDATE()");
Defensive patterns

Strategy: validation

Validate before calling

// Coerce empty to null, or require a real expression.
if (!string.IsNullOrEmpty(defaultValueSql))
{
    builder.HasDefaultValueSql(defaultValueSql);
}

Type guard

static bool IsValidDefaultValueSql(string? sql)
    => string.IsNullOrEmpty(sql) || sql.Trim().Length > 0;

Prevention

When it happens

Trigger: A column whose DefaultValueSql is set to string.Empty during migration diffing; typically from .HasDefaultValueSql("") or a convention/scaffolding path that assigns an empty string instead of null.

Common situations: Calling .HasDefaultValueSql(string.Empty) or wiring the value from a config/attribute that yields empty; scaffolding bugs producing empty SQL; conditional builder code that sets the SQL conditionally to empty.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:1234

    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,
                    column.Table.SchemaQualifiedName));
        }

        var property = column.PropertyMappings.First().Property;
        var valueConverter = GetValueConverter(property, typeMapping);
        columnOperation.ClrType
            = (valueConverter?.ProviderClrType
                ?? typeMapping.ClrType).UnwrapNullableType();

View on GitHub (pinned to 3a2006ef56)