2013-05-08 58 views
0

当我在第一个实体框架代码中定义一些POCO对象(实体)时,我遇到了一个问题。我有一个主要实体,比方说Entity_A,它只有一个属性,只有ID键)。其余实体(本例中的Entity_B)从它(子元素)继承,除了它们中的一些(Entity_C),它们从另一个实体(来自Entity_B,而不是来自Entity_A)继承。例如:实体框架代码优先:POCO的继承

public class Entity_A 
{ 
    public virtual Guid ID { get; set; } 
} 

// Entity_B has not primary key defined within as it inherits from Entity_A 
public class Entity_B : Entity_A 
{ 
    public virtual string propertyB1 { get; set; } 
    public virtual string propertyB2 { get; set; } 
    public virtual string propertyB3 { get; set; } 
} 

// Entity_C has not primary key defined within as it inherits from Entity_A through Entity_B 
public class Entity_C : Entity_B 
{ 
    public virtual string propertyC1 { get; set; } 
    public virtual string propertyC2 { get; set; } 
    public virtual string propertyC3 { get; set; } 
} 

所以执行它之后,将自动生成Entity_A,Entity_B,Entity_C表,但只为Entity_A和Entity_B表​​是正确的,但不适合Entity_C:

表Entity_A具有字段:
-ID

这是正确的。

表Entity_B具有字段:
-ID
-propertyB1
-propertyB2
-propertyB3

这是正确的,以及。

表Entity_C具有字段:
-ID
-propertyC1
-propertyC2
-propertyC3

这是不是我是正确的,作为Entity_C我期待以下字段:
- ID
-propertyB1
-propertyB2
-propertyB3
-propertyC1
-propertyC2
-propertyC3

我在做什么错?根本不支持继承的实体框架代码(版本4.1)?

在此先感谢。

回答

1

尝试添加记录到Entity_C,您会注意到记录也被添加到Entity_B和Entity_A。

这是表继承。一个Entity_C是一个Entity_B,为什么复制这些行?

+0

谢谢!我现在知道了。正如你所说,重复行是没有意义的。 – user1624552 2013-05-08 19:44:28