我有以下的(简称为清楚起见) - 一个枚举,与枚举的基类,和两个派生类设置枚举一个特定的值
I have the following (abbreviated for clarity) - an enum, a base class with that enum, and two derived classes that set the enum 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; }
}
我想要做的,是有实体框架5 DerivedOne和DerivedTwo,之间自动区分与MyEnum价值为基础的鉴别。我应该能够做到这一点的,按照惯例,每MyEnum == MyEnum.Value1重新presents DerivedOne,并MyEnum == MyEnum.Value2重新presents DerivedTwo。
What I want to do, is have Entity Framework 5 automatically distinguish between DerivedOne and DerivedTwo, with a MyEnum value based discriminator. I should be able to do this as, by convention, every MyEnum == MyEnum.Value1 represents DerivedOne, and MyEnum == MyEnum.Value2 represents DerivedTwo.
我想这在我的DbContext:
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);
}
}
不过,这将引发以下InvalidOperationException异常:
However, this throws the following InvalidOperationException:
这位前pression'X =>(转换(x.MyEnum)== 0)不是一个有效的属性EX pression。这位前pression应该重新present一个属性(...)
The expression 'x => (Convert(x.MyEnum) == 0)' is not a valid property expression. The expression should represent a property (...)
编辑:我相信我有一个稍远一点使用这样的:
I believe I got a little farther using this:
modelBuilder.Entity<MyBaseClass>().Map<DerivedOne>(m => m.Requires("MyEnum")
.HasValue((Int32)MyEnum.Value1));
现在我得到这个EntityCommandCompilationException:
Now I get this EntityCommandCompilationException:
在开始行(...)条件成员MyBaseClass.MyEnum'比'ISNULL =假'等条件映射碎片问题的映射。无论是去除MyBaseClass.MyEnum条件或映射中删除。
Problem in mapping fragments starting at line (...) Condition member 'MyBaseClass.MyEnum' with a condition other than 'IsNull=False' is mapped. Either remove the condition on MyBaseClass.MyEnum or remove it from the mapping.
我如何才能解决这个任何提示?谢谢!
Any hints on how I can solve this? Thanks!
据我所知,你不能这样做。做了明确的要求
指定disciminator只是给它一个名称
- 不能将它连接到你的财产。
As far as I know you cannot do that. Doing the explicit Requires
to specify the disciminator is only to give it a name
- not to connect it to your property.
据我所知,都会导致该错误(后下),你描述。如果要指定鉴别它必须是'automatic'one(至少我从来没有成功地定义这种方式)
As far as I know that always results in that error (later) that you're describing. If you want to specify discriminator it has to be 'automatic'one (at least I never managed to define it that way)
但是,你并不需要真的。在枚举和鉴别是内置
你回来的类型 - 基于鉴别值,EF / CF正在建设或者Base`或DerivedOne或DerivedTwo。
But you don't need that really. The 'enum' and discriminator is built into
the type you get back - based on the discriminator values, EF/CF is constructing either 'Base` or 'DerivedOne' or DerivedTwo.
因此,为了实现你愿意,你可以做以下什么...
So to implement what you want you can do the following...
public class MyBaseClass
{
[NotMapped()]
public virtual MyEnum MyEnum { get { return MyEnum.Base; } }
}
public class DerivedOne: MyBaseClass
{
public string OneProp { get; set; }
public override MyEnum MyEnum { get { return MyEnum.One; } }
}
public class DerivedTwo: MyBaseClass
{
public string TwoProp { get; set; }
public override MyEnum MyEnum { get { return MyEnum.Two; } }
}
或者只是使用是
代替(如果你的作品)...
Or just use is
instead (if it works for you)...
if (entity is MyBaseClass) // instead of enum
或查询的...
or Query by...
.OfType<MyBaseClass>();