实体框架 4.0 中的反序列化问题

发布于 2025-01-05 22:32:21 字数 377 浏览 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 enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

遗心遗梦遗幸福 2025-01-12 22:32:21

可能是序列化之前没有加载数据,但是当您在服务器端调试时,延迟加载导致数据被加载。

您可以尝试一些事情。

这将强制查询运行:

Employees.include("Departments.Branch").ToList();

这将显式加载实体:

context.Entry(Employees).Reference(u => u.Departments.Branch).Load();

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:

Employees.include("Departments.Branch").ToList();

This will explicitly load the entities:

context.Entry(Employees).Reference(u => u.Departments.Branch).Load();
晨曦÷微暖 2025-01-12 22:32:21

您可以尝试使用实体框架分析器并查看生成的查询是否具有有效数据。 http://efprof.com/

You could try to use Entity Framework profiler and see whether generated query is having valid data. http://efprof.com/

蓝颜夕 2025-01-12 22:32:21

来自 MSDN

Windows Communication Foundation (WCF) 无法直接序列化或反序列化代理,因为 DataContractSerializer 只能序列化和反序列化已知类型,而代理类型不是已知类型。当您需要序列化 ​​POCO 实体时,请禁用代理创建或使用 ProxyDataContractResolver 类将代理对象序列化为原始 POCO 实体。要禁用代理创建,请将 ProxyCreationEnabled 属性设置为 false。

除此之外,检查您是否使用可互操作对象引用?我也会尝试一下。请按照此处的说明进行设置向上

From MSDN

Windows Communication Foundation (WCF) cannot directly serialize or deserialize proxies because the DataContractSerializer can only serialize and deserialize known types, and proxy types are not known types. When you need to serialize POCO entities, disable proxy creation or use the ProxyDataContractResolver class to serialize proxy objects as the original POCO entities. To disable proxy creation, set the ProxyCreationEnabled property to false.

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

诗酒趁年少 2025-01-12 22:32:21

在 POCO 类 + 枚举和结构(所有这些都被序列化)上,您必须具有 IsReference=true 的 DataContract 属性:

[DataContract(Name = "Employee", Namespace = "http://MyNamespace", IsReference=true)]
class Employee
{
    [DataMember(Name="Branch")]
    public Branch _branch = new Branch();
            ... or ...
    [DataMember(Name="Branches")]
    public List<Branch> _branches = new List<Branch>();
}

[DataContract(Name = "Branch", Namespace = "http://MyNamespace", IsReference=true)]
class Branch { ... }

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:

[DataContract(Name = "Employee", Namespace = "http://MyNamespace", IsReference=true)]
class Employee
{
    [DataMember(Name="Branch")]
    public Branch _branch = new Branch();
            ... or ...
    [DataMember(Name="Branches")]
    public List<Branch> _branches = new List<Branch>();
}

[DataContract(Name = "Branch", Namespace = "http://MyNamespace", IsReference=true)]
class Branch { ... }

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.

究竟谁懂我的在乎 2025-01-12 22:32:21

在 POCO 类上,使用 [DataContract] 作为类,[DataMember] 作为属性,就像 Bahri Gungor 所说的那样。此外,您还可以编写一些方法,这些方法将由序列化器调用。检查 MSDN:OnDeserialized OnDeserializing, OnSerializedOnSerializing

在 WCF 服务中,您需要使用属性来声明要返回 EF-Proxy-Classes 的位置。

[ApplyDataContractResolver]
public POCO_CLASS GetById(int id){
    // your code here...
}

ApplyDataContractResolver 如下所示:

public class ApplyDataContractResolverAttribute : Attribute, IOperationBehavior {
    public ApplyDataContractResolverAttribute() {
    }

    public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters) {
    }

    public void ApplyClientBehavior(OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy) {
        var dataContractSerializerOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        dataContractSerializerOperationBehavior.DataContractResolver = new ProxyDataContractResolver();
    }

    public void ApplyDispatchBehavior(OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch) {
        var dataContractSerializerOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        dataContractSerializerOperationBehavior.DataContractResolver = new ProxyDataContractResolver();
    }

    public void Validate(OperationDescription description) {
        // Do validation.
    }
}

背景是 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 OnSerializing

In WCF-Service you need to use a Attribute, to declare, where you want to return EF-Proxy-Classes.

[ApplyDataContractResolver]
public POCO_CLASS GetById(int id){
    // your code here...
}

And the ApplyDataContractResolver looks like:

public class ApplyDataContractResolverAttribute : Attribute, IOperationBehavior {
    public ApplyDataContractResolverAttribute() {
    }

    public void AddBindingParameters(OperationDescription description, BindingParameterCollection parameters) {
    }

    public void ApplyClientBehavior(OperationDescription description, System.ServiceModel.Dispatcher.ClientOperation proxy) {
        var dataContractSerializerOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        dataContractSerializerOperationBehavior.DataContractResolver = new ProxyDataContractResolver();
    }

    public void ApplyDispatchBehavior(OperationDescription description, System.ServiceModel.Dispatcher.DispatchOperation dispatch) {
        var dataContractSerializerOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        dataContractSerializerOperationBehavior.DataContractResolver = new ProxyDataContractResolver();
    }

    public void Validate(OperationDescription description) {
        // Do validation.
    }
}

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

自由如风 2025-01-12 22:32:21

尝试在查询中使用 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文