2013-05-08 57 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