实体框架 4.0 中的反序列化问题
我正在使用 Entity Framework 4.0 开发一个应用程序。和WCF。
我正在返回对象列表(员工)
,该对象的导航属性是(部门),
并且部门还有一个导航属性(分支)
我将所有内容包括在内,因为
Employees.include("Departments.Branch");
现在问题所有分支相同的部门已设置在 WCF 上反序列化时为 null(第一个除外)。
我需要使用分支来实现某些绑定目的,请指导我如何摆脱这个问题。
这是实体的屏幕截图
I am working on an appliction, using Entity Framework 4.0. and WCF.
I am returning list of objects say (Employees)
and the navigational property of that object is say(Departments)
and Department has further a navigational property (Branch)
I am including everything as
Employees.include("Departments.Branch");
Now th issue all those departments whose Branch is same, is set to null(except the first one) upon deserializing on WCF.
I need to use the branch for some binding purposes, kindly guide me how should i get rid of this problem.
This is the screenshot of the entities
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能是序列化之前没有加载数据,但是当您在服务器端调试时,延迟加载导致数据被加载。
您可以尝试一些事情。
这将强制查询运行:
这将显式加载实体:
It may be that the data is not being loaded before serialisation, but when you debug on the server side, lazy loading causes the data to be loaded.
A couple of things that you could try.
This will force the query to run:
This will explicitly load the entities:
您可以尝试使用实体框架分析器并查看生成的查询是否具有有效数据。 http://efprof.com/
You could try to use Entity Framework profiler and see whether generated query is having valid data. http://efprof.com/
来自 MSDN
除此之外,检查您是否使用可互操作对象引用?我也会尝试一下。请按照此处的说明进行设置向上
From MSDN
Beyond that, check if you are using Interoperable Object References? I would try that as well. Follow the instructions here on how to set it up
在 POCO 类 + 枚举和结构(所有这些都被序列化)上,您必须具有 IsReference=true 的 DataContract 属性:
IsReference=true 确保所有指针在序列化期间都转换为引用。还要确保您的引用具有 DataMember 属性。
On your POCO classes + enumerations and structs (all of them that are serialized), you must have the DataContract attribute with IsReference=true:
It is the IsReference=true that makes sure that all of the pointers are converted into references during serialization. Also make sure your references have a DataMember attribute.
在 POCO 类上,使用
[DataContract]
作为类,[DataMember]
作为属性,就像 Bahri Gungor 所说的那样。此外,您还可以编写一些方法,这些方法将由序列化器调用。检查 MSDN:OnDeserialized OnDeserializing, OnSerialized 和 OnSerializing在 WCF 服务中,您需要使用属性来声明要返回 EF-Proxy-Classes 的位置。
ApplyDataContractResolver 如下所示:
背景是 WCF 使用 DataContractSerializer 进行序列化,并且 EF 构建代理类来跟踪更改。
这里是 ProxyDataContractResolver 的 MSDN 链接:MSDN
On the POCO-Classes use
[DataContract]
for the class,[DataMember]
for properties, like Bahri Gungor said. In addition you can write some Methods, which will be called by the Serializer. Check MSDN for: OnDeserialized OnDeserializing, OnSerialized and OnSerializingIn WCF-Service you need to use a Attribute, to declare, where you want to return EF-Proxy-Classes.
And the ApplyDataContractResolver looks like:
Background is that the WCF serialise with an
DataContractSerializer
and EF builds Proxy-Classes to track the changes.Here a link to MSDN for
ProxyDataContractResolver
: MSDN尝试在查询中使用
.Include()
来加载相关对象,并在元数据中使用[Ininclude]
属性,以允许这些相关对象被序列化并发送到客户。此处讨论了此问题(或至少类似问题)
Try to use both
.Include()
on the query to load related objects and the[Include]
attribute in the metadata to allow those related objects to be serialized and sent to the client.This issue (or at least similar) is discussed here