7

I have just upgraded to Entity Framework Core 8 from 7. Using code first, Add-Migration is now returning the following new warning:

Microsoft.EntityFrameworkCore.Model.Validation[20601]
The 'MyEnum' property 'MyEnumPropertyID' on entity type 'MyTableEntity' 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 'Error', since this is the CLR default for the 'MyEnum' type.
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. See https://aka.ms/efcore-docs-default-values for more information.

Question

  1. What does this error mean? What is a sentinel value in SQL Server? (yes I followed the Microsoft link, but no info there)
  2. How do I set a sentinel value for the enum property? (I don't want to use nullable)

Code

MyTableEntity is configured to use MyEnum as shown here:

public partial class MyTableEntity
{
    // primary key and other properties not shown

    // This is not a foreign key to another table, just an enum 
    public MyEnum MyEnumPropertyID { get; set; }   
}

The model builder defines the default as shown

modelBuilder.Entity<MyTableEntity>(entity =>
{
    //other properties not shown
    entity.Property(e => e.MyEnumPropertyID)
        .IsRequired()
        .HasDefaultValue((object)MyEnum.Default)
        .HasConversion<int>(); //
}

And the enum is just a plain old garden variety enum

public enum MyEnum { Error, Default, HaveAGreatDay }

1 Answer 1

8

Because default(MyEnum} == (MyEnum)0 == MyEnum.Error. EF can't tell if you have explicitly assigned MyEnum.Error.

You can reserve a value in your enum that is invalid. Then inform EF to consider this value as an unassigned "sentinal". eg .HasSentinel(MyEnum.Unspecified). In which case the value will not be passed to the database, and the database default value will be used.

Or you store the property value in a MyEnum? backing field, then EF can tell the difference between unassigned and any explicit value.

4
  • Thanks @Jeremy. In my case I use the MyEnum.Error (whose int is 0) to indicate that the enum has not been set. Does this mean I use .HasSentinal(MyEnum.Error)? But the sentinal then being used is the natural default of the enum (value=0). This means the '0' will not be passed to the database, and the default (0) will be used? Sounds odd. If I set my default to .HasDefaultValue((object)MyEnum.Default) (not 0) and .HasSentinal(MyEnum.Error) (0) then will this work? Or must I now define a new MyEnum.Unspecified in the enum? Thanks for your help Commented Feb 12 at 0:58
  • 1
    Right, assuming you never want to insert an .Error. When EF detects this sentinal for new records, it will leave the column out of the insert sql statement. The database will then provide the column default, which should have been defined based on your .HasDefaultValue((MyEnum)1) when you create and apply migrations. Commented Feb 12 at 1:05
  • 1
    .HasSentinal(default) will result in exactly the same behaviour as previous versions, but now you need to be explicit to avoid the warning message when the database column default is not default. Commented Feb 12 at 1:08
  • (there is 3rd solution) Or, you can build your habit of always putting your default Enum option as the 1st option, and setting its value to 0, if you don't want to have an extra option for unspecified option in your Enum. Commented Jul 6 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.