我使用實體框架一對一的關係在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」:
的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?誰可以幫我感謝
顯示你'GetAppByTypeidContentid'實現 - 尤其是部分查詢EF? –
我提供的GetAppByTypeidContentid代碼高於 – artwl