2017-09-03 54 views
0

我有3個類A,B和AB。 A類和B類相互獨立。但AB依賴於A和B.我希望使用實體框架代碼優先實現A和B之間的「零或一到零或一個」關係。實體框架中的零或一到零或一個與組合鍵的關係

有人能告訴我爲什麼這不起作用嗎?還是我完全錯了?謝謝!

public class A 
{  
    public int Id { get; set; } 
    public string PropertyName { get; set; } 

    public virtual AB AB { get; set; }   
} 

public class B 
{  
    public int Id { get; set; } 
    public string PropertyName { get; set; } 

    public virtual AB AB { get; set; }   
} 

public class AB 
{ 
    [Key, ForeignKey("A"), Column(Order = 0)] 
    public int AId { get; set; } 

    [Key, ForeignKey("B"), Column(Order = 1)] 
    public int BId { get; set; } 

    public virtual A A { get; set; } 

    public virtual B B { get; set; } 
} 

我發現了這樣的錯誤:

AB_B_Source: : Multiplicity is not valid in Role 'AB_B_Source' in relationship 'AB_B'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

AB_A_Source: : Multiplicity is not valid in Role 'AB_A_Source' in relationship 'AB_A'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

回答

1

基於所編輯的模型中,導航屬性AB中的每個類(A和B)可能是問題,因爲爲指向一個AB對象您需要同時使用兩把鑰匙。 實體AB可能映射了一個複合主鍵,並且您想要的關係應該已經使用該實體中的屬性A和B來定義。

讓我知道它的工作, 有一個愉快的一天

+0

謝謝您的回答。事實上,我一直試圖從現有的更復雜的模型中創建一個簡化的模型,並且我省略了一些部分。 「GranteeFingerPrintInfo」類實際上是「AB」,應該刪除類「A」中屬性「Id」的註釋。所以註釋在真實模型中不是問題。請考慮這些變化,我編輯過mu問題。謝謝! –

相關問題