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
- Pass a real SQL expression to HasDefaultValueSql("GETUTCDATE()").
- If no default SQL is intended, remove the call rather than passing empty.
- Coerce empty strings to null before assigning DefaultValueSql.
- 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
- Never pass string.Empty to HasDefaultValueSql.
- Sanitize config-driven SQL strings to null when empty.
- Inspect scaffolded migrations for empty default-SQL fragments.
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
- The computed column SQL has not been specified for the…
- The default value has not been specified for the column
- A seed entity for entity type
- A synchronous store management operation was performed and…
- Cosmos-specific methods can only be used when the context…
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)