With EntityFramework Core how can I limit a property to some values with the modelBuilder ?
In SQL it's :
CONSTRAINT chk_Frequency CHECK (Frequency IN ('Daily', 'Weekly', 'Monthly', 'Yearly'))
Thanks,
You can use an enum
public enum Frequency
{
Daily,
Weekly,
Monthly,
Yearly
}
Then use EF Core value conversion with the EnumToStringConverter. This will work:
modelBuilder
.Entity<Rider>()
.Property(e => e.Mount)
.HasConversion<string>();
It will not create the constraint in the database but it will put the enum type on the mapped property. If a bad value comes from the database it will map to the enum's default value.