2014-02-28 110 views
0

我有一個用戶模型與市,鎮,區表的導航屬性:導航性能

public class User 
{ 
    ... 
    public virtual Location_City City { get; set; } 
    public virtual Location_Town Town { get; set; } 
    public virtual Location_District District { get; set; } 
} 

我想在一個單獨的類組這些屬性,並重新使用該類相反,如下圖所示:

public class Location 
{ 
    public virtual Location_City City { get; set; } 
    public virtual Location_Town Town { get; set; } 
    public virtual Location_District District { get; set; } 
} 

public class User 
{ 
    ... 
    public Location Location { get; set; } 
    public Location ContactPersonLocation {get; set;} 
} 

我試圖其中I除去虛擬位置類,並設置各種組合用戶類的導航屬性的位置屬性,而不是,但我收到以下錯誤:

EntityType 'Location' has no key defined. Define the key for this EntityType. Locations: EntityType: EntitySet 'Locations' is based on type 'Location' that has no keys defined.

是否有可能有導航屬性在一個單獨的類或是否需要創建一個單獨的表?

回答

2

如果您不想爲位置創建單獨的表,那麼您應該使用Complex Type(它們沒有密鑰並且與具有複雜類型屬性的實體存儲在同一個表中)。但這裏是另一個問題 - 作爲文檔說:

Complex type cannot contain navigation properties.

所以,答案是顯而易見的 - 你應該保持位置創建單獨的結表。並且位置應該是具有密鑰的實體。

+1

謝謝,我確實創建了一個聯結表:) –