Entity object's navigation property set to null after Deserialization

妖精的绣舞 提交于 2019-12-11 04:35:15

问题


I am working on a WPF application, the architect is

WPF->WCF->DAL

the issue is when i call the WCF method, it returns the object. Everything upto WCF level works just fine, but while returning the object, after WCF my navigation properties are setting to null.

i am returning the object(POCO) object of Department class, and its navigation property is Employees. I verfied the [DataMember] attribute, this is not the case. But as soon as it reaches the MainUI its Employees property is setting to null.

The code of my navigation property looks like that

Thanks


回答1:


I suppose this happens because the navigation properties are lazy loaded. When you try to access them they cannot be loaded because the context is disposed. You should load them eagerly or explicitly when you load the main property.

Eager loading:

from d in context.Department.Include("Employees")
select d;

Explicit loading:

var departments = (from d in context.Department
                   select d).ToList();
departments.ForEach(e => e.EmployeesReference.Load());

EDIT AFTER QUESTION EDIT

The problem could be the line:

department.FirstOrDefault().Employees = employees.ToFixupCollection();

Try changing it to:

department.FirstOrDefault().Employees = employees.ToList<Employee>();


来源:https://stackoverflow.com/questions/8265789/entity-objects-navigation-property-set-to-null-after-deserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!