实体对象的导航属性在反序列化后设置为null [英] Entity object's navigation property set to null after Deserialization
问题描述
我正在开发一个WPF应用程序,架构师是
I am working on a WPF application, the architect is
WPF-> WCF-> DAL
WPF->WCF->DAL
问题是当我调用WCF方法,它返回对象。一切WCF级别都可以正常工作,但返回对象时,WCF之后,我的导航属性设置为null。
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.
我正在返回Department类的对象(POCO),其导航属性为Employees。我对[DataMember]属性进行了验证,情况并非如此。但是一到达MainUI,它的Employees属性设置为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
谢谢
推荐答案
我想这是因为导航属性被延迟加载。当您尝试访问它们时,由于上下文已被处理,因此无法加载它们。加载主要财产时,您应该加载他们。
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.
渴望加载:
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>();
这篇关于实体对象的导航属性在反序列化后设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!