Reputation: 57373
I'm using Entity Framework.
My entity has a DayOfWeek property of type string.
How can I limit its values to only "Sunday", "Monday", "Tuesday", etc?
Upvotes: 0
Views: 684
Reputation: 364409
There is no simple way. In case of ObjectContext API you can override SaveChanges
in your ObjectContext
implementation:
public override int SaveChanges(SaveOptions options)
{
var entities = ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
.Select(e => e.Entity)
.OfType<YourEntityType>();
foreach (var entity in entities)
{
// Here you can validate your property - entity is of your type
}
return base.SaveChanges(options);
}
The problem is that EF doesn't have support for custom scalar types and it also doesn't support enums in case of replacing your values with int.
Another way is enforcing this by your custom validation in business logic.
Upvotes: 2