Using runtime-evaluated expressions (e.g., DateTime.UtcNow, Guid.NewGuid()) as default values for a property causes the
value to be hardcoded as a static value in the database schema, rather than computed per row. This affects HasDefaultValue() in Fluent
API configurations and defaultValue in migration files.
Starting with EF Core 9, this also throws an InvalidOperationException for the PendingModelChangesWarning warning when
applying migrations, because the default value expression evaluates to a new value on each model build, making migrations non-deterministic.
Replace HasDefaultValue() with HasDefaultValueSql() in Fluent API configurations, and replace defaultValue
with defaultValueSql in migration files. This instructs EF Core to let the database engine generate the value at insertion time, using
the equivalent function for your database provider:
| Client-evaluated expression | SQL Server | PostgreSQL | MySQL / MariaDB | SQLite | Oracle |
|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(no offset-aware type; closest is |
(no offset-aware type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(no native function) |
|
|
|
(no native function) |
|
|
(no native function) |
(no native function) |
Some provider-specific quirks to be aware of when picking a replacement:
UUID() returns a version 1 UUID, not version 4; MariaDB additionally provides a dedicated UUID_v4().SYS_GUID() returns a raw 16-byte value, not formatted as a standard hyphenated UUID string.DateTimeOffset values don’t map cleanly to a single database type.
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasKey(x => x.StudentId);
builder.Property(x => x.EnrollmentDate)
.IsRequired(true)
.HasDefaultValue(DateTime.Now); // Noncompliant
}
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasKey(x => x.StudentId);
builder.Property(x => x.EnrollmentDate)
.IsRequired(true)
.HasDefaultValueSql("GETDATE()"); // SQL Server example — see the table above for other providers
}
migrationBuilder.AddColumn<DateTime>(
name: "EnrollmentDate",
table: "Students",
nullable: false,
defaultValue: DateTime.UtcNow); // Noncompliant
migrationBuilder.AddColumn<DateTime>(
name: "EnrollmentDate",
table: "Students",
nullable: false,
defaultValueSql: "GETUTCDATE()"); // SQL Server example — see the table above for other providers
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasKey(x => x.StudentId);
builder.Property(x => x.TranscriptId)
.HasDefaultValue(Guid.NewGuid()); // Noncompliant
}
public void Configure(EntityTypeBuilder<Student> builder)
{
builder.HasKey(x => x.StudentId);
builder.Property(x => x.TranscriptId)
.HasDefaultValueSql("NEWID()"); // SQL Server example — see the table above for other providers
}