2011-12-02 81 views
1

我使用實體框架一對一的關係在WCF服務器端,主要代碼:爲什麼Entity Framework WCF客戶端中的一對一關係爲空?

[DataContract] 
public class AppType 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [DataMember] 
    public int TypeID { get; set; } 

    [DataMember] 
    public string TypeName { get; set; } 
} 

[DataContract] 
public class App 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [DataMember] 
    public long AID { get; set; } 

    [DataMember] 
    [ForeignKey("AppType")] 
    public int TypeID { get; set; } 

    [DataMember] 
    public DateTime DateAdded { get; set; } 

    [DataMember] 
    public virtual AppType AppType { get; set; } 
    //... 
} 

因爲序列化的問題,我必須關閉惰性加載和proxyclass在EF:

this.Configuration.LazyLoadingEnabled = false; 
this.Configuration.ProxyCreationEnabled = false; 

現在, WCF的客戶端可以工作,但我不能用「應用」獲得「的AppType」: enter image description here

的GetAppByTypeidContentid方法是:

public App GetAppByTypeidContentid(string contentid, int typeid) 
{ 
    using (var db = new TagDbContext()) 
    { 
     try 
     { 
      var app = db.Apps.SingleOrDefault(a => a.ContentID.Trim() == contentid.Trim() && a.TypeID == typeid); 
      if (app != null) 
      { 
       return app; 
      } 
      else 
      { 
       throw new Exception("App not exist!"); 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.Message); 
     } 
    } 
} 

是否必須使用DTO?誰可以幫我感謝

+0

顯示你'GetAppByTypeidContentid'實現 - 尤其是部分查詢EF? –

+0

我提供的GetAppByTypeidContentid代碼高於 – artwl

回答

2

您關閉延遲加載,所以你必須告訴EF加載AppType

var app = db.Apps.Include("AppType") 
      .SingleOrDefault(a => a.ContentID.Trim() == contentid.Trim() && a.TypeID == typeid); 
+0

我收到錯誤: 接收到http://www.testtag.com/AppTagService.svc的HTTP響應時發生錯誤。這可能是由於服務端點綁定不使用HTTP協議。這也可能是由於HTTP請求上下文被服務器取消(可能由於服務關閉)而中止 。有關更多詳細信息,請參閱服務器日誌文件 。 – artwl

+0

這意味着您的服務端有問題,或者您沒有顯示所有代碼。首先改變你的類來使用:'[DataContract(IsReference = true)]'。如果它無法打開服務上的[WCF跟蹤](http://msdn.microsoft.com/zh-cn/library/ms733025.aspx)以獲取詳細的錯誤說明。 –

相關問題