Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

EF Core warns that a bool or enum default has no sentinel value

Your error

is configured with a database-generated default, but has no configured sentinel value.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The default is only there for migrations, but EF treats it as generated

Keep the default for the schema and tell EF to always send the property's value.

Fix
modelBuilder.Entity<Account>()
    .Property(e => e.IsActive)
    .HasDefaultValue(true)
    .ValueGeneratedNever();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

EF Core 8 to 11 log "is configured with a database-generated default, but has no configured sentinel value" for a non-nullable bool or enum with HasDefaultValue or HasDefaultValueSql. EF skips the column whenever the property holds false or 0, so the database default wins. Use ValueGeneratedNever, HasSentinel, a nullable type or a nullable backing field.

  • Applies to EF Core 8, 9, 10 and 11.0, SQL Server, PostgreSQL and SQLite providers
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

The warning names the type, the property and the entity. Find where that property gets its default and note whether you wrote it or a scaffold did; then ask whether your code ever needs to insert false (or the enum's 0 value) on purpose.

Run this first
grep -rn "HasDefaultValue" --include=*.cs .

Then find the line you see in this list and open its fix:

  1. 1The default is only there for migrations, but EF treats it as generatedThe column default exists so the migration can fill existing rows or so other apps get a value, and your code always sets the property itself
  2. 2false or the first enum member is a real value you need to insertRows saved with false (or the enum's 0 member) come back with the database default, for example every account ends up active
  3. 3The column default is an SQL expression EF cannot read as a valueThe property is configured with HasDefaultValueSql, for example in a DbContext scaffolded from an existing database

What does the error look like?

  • is configured with a database-generated default, but has no configured sentinel value.
  • The database-generated default will always be used for inserts when the property has the value
  • Consider using a nullable type, using a nullable backing field, or setting the sentinel value for the property to ensure the database default is used when, and only when, appropriate.

Cause 1 of 3

The default is only there for migrations, but EF treats it as generated

You seeThe column default exists so the migration can fill existing rows or so other apps get a value, and your code always sets the property itself

Check

EF Core raises the no configured sentinel value warning because a property with a database default uses the CLR default of its type as the marker for not set. A bool left at false, or an enum left at its 0 member, is skipped in the INSERT, and the database writes its own default instead.

Check
grep -rn -A2 "Property(e => e.IsActive)" --include=*.cs .

Fix

Keep the default for the schema and tell EF to always send the property's value. Microsoft's EF8 notes show exactly this pattern with ValueGeneratedNever.

Fix
modelBuilder.Entity<Account>()
    .Property(e => e.IsActive)
    .HasDefaultValue(true)
    .ValueGeneratedNever();

Verify

The warning is gone at startup, and an Account saved with IsActive = false stays false in the table.

Verify
dotnet run

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

false or the first enum member is a real value you need to insert

You seeRows saved with false (or the enum's 0 member) come back with the database default, for example every account ends up active

Check

With a default of true on a bool, Microsoft's docs describe the column always ending up true, whatever the property says. Enums with a non-zero default lose their 0 member the same way. EF needs a different value to mean not set.

Check
SELECT IsActive, COUNT(*) FROM Accounts GROUP BY IsActive;

Fix

For enums, add an Unspecified member, make it the sentinel and the property's starting value. For bools, give the property a nullable backing field: EF reads and writes the field, and null means the setter was never called.

Fix
// enum: only Level.Unspecified triggers the database default
modelBuilder.Entity<Course>()
    .Property(e => e.Level)
    .HasDefaultValue(Level.Intermediate)
    .HasSentinel(Level.Unspecified);

public class Course
{
    public int Id { get; set; }
    public Level Level { get; set; } = Level.Unspecified;
}

// bool: a nullable backing field tells "never set" apart from false
public class Account
{
    public int Id { get; set; }
    private bool? _isActive;
    public bool IsActive
    {
        get => _isActive ?? false;
        set => _isActive = value;
    }
}

Verify

Insert one row with the value set to false (or Level.Beginner) and one without setting it. The first keeps your value; the second gets the database default.

Verify
dotnet test --filter Defaults

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The column default is an SQL expression EF cannot read as a value

You seeThe property is configured with HasDefaultValueSql, for example in a DbContext scaffolded from an existing database

Check

EF Core's model validator fires for a non-nullable bool or enum when HasDefaultValueSql is set at all, because the SQL text gives EF no value to compare with. It stays quiet only when the property is ValueGeneratedNever, has a nullable backing field, or has a sentinel configured.

Check
grep -rn "HasDefaultValueSql" --include=*.cs .

Fix

Follow the warning's own advice and make the property nullable, so null (not false) means use the default. If the app always sets the value, mark it ValueGeneratedNever as in the first cause, or give it a nullable backing field as in the second.

Fix
public class Account
{
    public int Id { get; set; }
    public bool? IsActive { get; set; }   // null lets the SQL default apply
}

modelBuilder.Entity<Account>()
    .Property(e => e.IsActive)
    .HasDefaultValueSql("1");

Verify

Build and start the app with logging at Warning level. No sentinel warning appears for that property.

Verify
dotnet run

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Add an integration test that saves false and the first enum member for every property that has a database default, and reads them back.
  • Give new enums an Unspecified member at 0 before you attach a database default to them.
  • Treat EF warnings as errors in a test environment so a new default without a sentinel fails the build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: DCOR.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.