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
- What does this error mean? What is a sentinel value in SQL Server? (yes I followed the Microsoft link, but no info there)
- 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 }