2012-11-23 45 views
0

我遇到了實體框架4.1和關係的問題。流利的api和關係有問題

這裏是我的課:

[Table("PROJTABLE")] 
    public class Certifikat { 
     [Key] 
     public long Recid { get; set; } 

     public String Projid { get; set; }   
     public virtual StandardAndScope StandardInfo { get; set; } 
} 

[Table("DS_CRT_PROJSTANDARDSCOPE")] 
    public class StandardAndScope { 
     //[Key] 
     //public long RECID { get; set; } 
     [Key] 
     public String Projid { get; set; } 

     public String Standard { get; set; } 
     public String Scope { get; set; } 
    } 

因爲我沒有對數據庫沒有控制,我不能改變的鑰匙和ID的支持公約,我被困在此設置。

我的問題是,Certifikat可以有一個StandardAndScope的關係。兩個表中的關鍵字都稱爲Projid - 但這並非嚴格意義上數據庫中的主鍵。

我真正想要的是說:「certifikat C對c.Projid = s.Projid加入standardandscope的」

如何做到這一點用流利的API?

+0

兩個實體是否都引用一個'Project'實體? –

+0

Dynde。我的問題非常相關。如果他們引用同一個Project實體,則可以使用這些導航屬性。 –

+0

對不起,沒有注意到你的評論。實際上沒有項目實體(如果我有機會殺死製作該數據庫的人)。不過,我發現,如果不同的列具有特定的值(「crt」),那麼只有一對多的關係,但我不知道是否可以告訴實體框架只映射設置了該值的行 – Dynde

回答

1

我認爲你正在尋找的東西是這樣的:

var result = dataContext.Certifikats 
    .Join(dataContext.StandardAndScopes, 
     c => c.Projid, 
     s => s.Projid, 
     (c, s) => new 
     { 
      Certifikat = c, 
      StandardAndScope = s 
     }); 

哪裏dataContext是您DbContext類的一個實例。

+0

好吧,這不會完全利用EF的導航屬性的優勢。我不需要依賴我需要的時候加入數據。 – Dynde

+1

@Dynde,我沒有看到你的類中定義的任何導航屬性。 – RePierre

+0

真的嗎?我認爲虛擬StandardAndScope StandardInfo是一個導航屬性。然而,我將不得不求助於這種類的連接,而不是使用導航屬性。 – Dynde