2011-12-02 87 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)以获取详细的错误说明。 –

相关问题