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

Thrown by MigrationsModelDiffer.Initialize when a column's `DefaultValueSql` is an empty string (length 0). An empty SQL fragment is treated as 'unspecified but required', so EF refuses to generate a column definition without a real SQL expression.

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 dbf9771522)

Solutions

  1. Pass a real SQL expression: `.HasDefaultValueSql("GETUTCDATE()")`.
  2. If no SQL default is wanted, remove the `.HasDefaultValueSql(...)` call entirely (use `null`).
  3. Validate the SQL string is non-empty before assigning it in generated differ code.

Example fix

// before
modelBuilder.Entity<Foo>().Property(x => x.Created).HasDefaultValueSql("");
// after
modelBuilder.Entity<Foo>().Property(x => x.Created).HasDefaultValueSql("GETUTCDATE()");
Defensive patterns

Strategy: validation

Validate before calling

// Reject empty default-value SQL before applying config.
var sql = GetDefaultValueSql(); // your source
if (sql is { Length: 0 }) throw new ArgumentException("DefaultValueSql must be non-empty or null.");
if (sql is not null) builder.HasDefaultValueSql(sql);

Prevention

When it happens

Trigger: Configuring `.HasDefaultValueSql("")` (empty string) and then running `EnsureCreated`/`Migrate`/scaffolding, or building a column operation that sets DefaultValueSql to "" in custom differ code.

Common situations: Passing an empty/default string into `.HasDefaultValueSql(someVariable)` where the variable is empty; refactoring that leaves `string.Empty` in fluent config; template-generated migration code with blank SQL.

Related errors


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