2013-06-19 223 views
0

我使用實體框架創建了一個WCF服務。實體框架:獲取相關實體

我有2個表:劇院和地點。地點作爲劇院中的外鍵。

我的方法:

public theater[] GetTheaters() 
{ 

    using (Entities context = new Entities()) 
    { 
     return context.theater.ToArray(); 

    } 
} 

我不得不刪除從「虛擬」關鍵字「公共虛擬局部性地區{獲取;集;}」在我的戲劇課。否則,我得到一個CommunicationException。

但是,當我這樣做,我讓我的劇場名單,但局部性空...

我怎樣才能得到當地?

感謝

我的模型類(我也有其他實體):

public partial class locality 
    { 
     public locality() 
     { 
      this.theater = new HashSet<theater>(); 
     } 

     public int idLocality { get; set; } 
     public int npa { get; set; } 
     public string locality1 { get; set; } 

     public ICollection<theater> theater { get; set; } 
    } 


    public partial class theater 
    { 
     public theater() 
     { 
      this.session = new HashSet<session>(); 
     } 

     public int idTheater { get; set; } 
     public string name { get; set; } 
     public string address { get; set; } 
     public int idLocality { get; set; } 
     public double latitude { get; set; } 
     public double longitude { get; set; } 
     public int seats { get; set; } 
     public string phone { get; set; } 
     public string email { get; set; } 
     public bool threeD { get; set; } 

     public locality locality { get; set; } 
     public ICollection<session> session { get; set; } 
    } 

以下是錯誤,我得到:

「對象圖表類型‘地方’包含週期和如果參考跟蹤被禁用無法序列

編輯:

我找到的解決方案:

在我所在的班級裏,我收藏了劇院。

我不得不添加 「私人像這樣的setter:

」 公共ICollection的劇場{獲得;私人設置; }」

所以它的工作原理,但我仍然有一個問題,我無法訪問從當地實體劇院了。(沒有更多的雙向)

+0

爲您的模型類發佈更多代碼。您可能在該部分有問題 – Omar

回答

0

可以使用eager loading or explicit loading隨着預先加載您使用Include擴展方法:

return context.Theater.Include(t => t.Locality).ToArray(); 
+0

謝謝。我已經試過這個,但我得到一個CommunicationException ...我需要在我的EDMX模型的屬性中設置「LazyLoading Enabled」爲False嗎?並在劇場課程中將「虛擬」關鍵字從「public virtual locality locality {get; set;}」保留下來? – user2157493

+0

是的,在處理需要序列化的類型時,仍然應該禁用延遲加載。導航屬性上的虛擬關鍵字是延遲加載的要求之一,因此刪除它將禁用它,但您應該明確地將其關閉(或完全創建代理)。 – devdigital

+0

這是我的錯誤與測試客戶端(對不起,部分法語...):https://dl.dropboxusercontent.com/u/9197067/commerror.png – user2157493

0

如果要強制相關實體加載,你可以使用Include method這樣做默認情況下,相關的實體延遲加載

你的榜樣將b。 E:

​​
0

你錯過了正確的註釋來創建關係。請參閱下面的代碼。 (或者使用FluentAPI自己創建關係)

查找[Key][ForeignKey]註釋以及virtual關鍵字。

public partial class locality 
    { 
     public locality() 
     { 
      //this.theater = new HashSet<theater>(); 
     } 

     [Key] 
     public int idLocality { get; set; } 

     public int npa { get; set; } 
     public string locality1 { get; set; } 

     public virtual ICollection<theater> theaters { get; set; } 
    } 


    public partial class theater 
    { 
     public theater() 
     { 
      //this.session = new HashSet<session>(); 
     } 

     [Key] 
     public int idTheater { get; set; } 

     public string name { get; set; } 
     public string address { get; set; } 
     public int idLocality { get; set; } 
     public double latitude { get; set; } 
     public double longitude { get; set; } 
     public int seats { get; set; } 
     public string phone { get; set; } 
     public string email { get; set; } 
     public bool threeD { get; set; } 

     [ForeignKey("idLocality")] 
     public virtual locality locality { get; set; } 
     //public ICollection<session> session { get; set; } 
    } 
+0

謝謝,我找到了另一個解決方案,但如果我仍然有問題,我會查看這個 – user2157493

+0

您可以發佈您的其他解決方案作爲答案,並選擇它作爲解決方案 – Omar

+0

我是新來的所以我不能添加回答8小時後問:) – user2157493