问题
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