[英]Entity object's navigation property set to null after Deserialization
我正在開發WPF應用程序,架構師是
WPF-> WCF-> DAL
問題是當我調用WCF方法時,它返回對象。 WCF級別以上的所有內容都可以正常工作,但在返回對象時,在WCF之后我的導航屬性設置為null。
我正在返回Department類的對象(POCO)對象,其導航屬性是Employees。 我驗證了[DataMember]屬性,但事實並非如此。 但是當它到達MainUI時,其Employees屬性設置為null。
我的導航屬性的代碼如下所示
謝謝
我想這是因為導航屬性是延遲加載的。 當您嘗試訪問它們時,無法加載它們,因為上下文已被釋放。 加載主屬性時,應該熱切地或明確地加載它們。
渴望加載:
from d in context.Department.Include("Employees")
select d;
顯式加載:
var departments = (from d in context.Department
select d).ToList();
departments.ForEach(e => e.EmployeesReference.Load());
編輯后編輯
問題可能是這樣的:
department.FirstOrDefault().Employees = employees.ToFixupCollection();
嘗試將其更改為:
department.FirstOrDefault().Employees = employees.ToList<Employee>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.