I have the following (in short for clarity) - an enumeration, a base class with this enumeration, and two derived classes that set the enumeration to a specific value.
public enum MyEnum { Value1, Value2 } public class MyBaseClass { public MyEnum { get; protected set; } } public class DerivedOne: MyBaseClass { public DerivedOne { MyEnum = MyEnum.Value1; } } public class DerivedTwo: MyBaseClass { public DerivedTwo { MyEnum = MyEnum.Value2; } }
I want Entity Framework 5 to automatically distinguish between DerivedOne and DerivedTwo, with a discriminator based on MyEnum values . I should be able to do this because, by convention, each MyEnum == MyEnum.Value1 represents DerivedOne, and MyEnum == MyEnum.Value2 represents DerivedTwo.
I tried this in my DbContext:
public class MyDbContext : DbContext { DbSet<MyBaseClass> MyBaseClass { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<MyBaseClass>() .Map<DerivedOne>(m => m.Requires(x => x.MyEnum == MyEnum.Value1)); base.OnModelCreating(modelBuilder); } }
However, this raises the following InvalidOperationException:
The expression 'x => (Convert (x.MyEnum) == 0)' is not a valid property expression. An expression must represent a property (...)
Edit: I believe I used this a bit:
modelBuilder.Entity<MyBaseClass>().Map<DerivedOne>(m => m.Requires("MyEnum") .HasValue((Int32)MyEnum.Value1));
Now I get this EntityCommandCompilationException:
Problem with displaying fragments starting from the line (...) The status element "MyBaseClass.MyEnum" with a condition other than "IsNull = False" is displayed. Either remove the condition on MyBaseClass.MyEnum, or remove it from the mapping.
Any hints on how I can solve this? Thanks!