S8718 — Client-evaluated default values should use database functions

Language
C#
Type
Bug
Severity
Blocker
Tags
entity-framework-core, entity-framework, database, orm

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.

Why is this an issue?

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.

How to fix it

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

DateTime.Now

GETDATE()

now()

NOW()

datetime('now','localtime')

SYSDATE

DateTime.UtcNow

GETUTCDATE()

now() AT TIME ZONE 'utc'

UTC_TIMESTAMP()

CURRENT_TIMESTAMP

SYS_EXTRACT_UTC(SYSTIMESTAMP)

DateTime.Today

CAST(GETDATE() AS DATE)

CURRENT_DATE

CURDATE()

date('now','localtime')

TRUNC(SYSDATE)

DateTimeOffset.Now

SYSDATETIMEOFFSET()

now()

(no offset-aware type; closest is CURRENT_TIMESTAMP)

(no offset-aware type)

SYSTIMESTAMP

DateTimeOffset.UtcNow

SYSUTCDATETIME()

now() AT TIME ZONE 'utc'

UTC_TIMESTAMP()

CURRENT_TIMESTAMP

SYS_EXTRACT_UTC(SYSTIMESTAMP)

Guid.NewGuid()

NEWID()

gen_random_uuid()

UUID()

(no native function)

SYS_GUID()

Guid.CreateVersion7()

(no native function)

uuidv7() (PostgreSQL 18+)

UUID_v7() (MariaDB 11.7+ only)

(no native function)

(no native function)

Some provider-specific quirks to be aware of when picking a replacement:

Code examples

Noncompliant code example


public void Configure(EntityTypeBuilder<Student> builder)
{
    builder.HasKey(x => x.StudentId);
    builder.Property(x => x.EnrollmentDate)
        .IsRequired(true)
        .HasDefaultValue(DateTime.Now); // Noncompliant
}

Compliant solution


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
}

Noncompliant code example


migrationBuilder.AddColumn<DateTime>(
    name: "EnrollmentDate",
    table: "Students",
    nullable: false,
    defaultValue: DateTime.UtcNow); // Noncompliant

Compliant solution


migrationBuilder.AddColumn<DateTime>(
    name: "EnrollmentDate",
    table: "Students",
    nullable: false,
    defaultValueSql: "GETUTCDATE()"); // SQL Server example — see the table above for other providers

Noncompliant code example


public void Configure(EntityTypeBuilder<Student> builder)
{
    builder.HasKey(x => x.StudentId);
    builder.Property(x => x.TranscriptId)
        .HasDefaultValue(Guid.NewGuid()); // Noncompliant
}

Compliant solution


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
}

Resources

Documentation

↑ Back to top