实体框架和 DataContractSerializer

发布于 2024-11-29 18:17:29 字数 412 浏览 1 评论 0原文

我一直在阅读有关使用 Linq toEntity 和不同可能的序列化程序在实体框架上下文中序列化实体图的内容:Binary、XmlSerializer 和 DataContractSerializer。 据我了解,二进制文件和 XmlSerializer 序列化实体而无需其关系。如果关系被序列化,则会由于生成的 xml 文件结构(对于 XmlSerializer)的性质而导致问题。 DataContractSerializer 会在整个深度上序列化图表,除非禁用延迟加载。

我的问题是:我想序列化图表的一部分。例如,如果我有一个实体 A 和三个相关实体 B、C 和 D,则只有 B 和 D 会与 A 一起序列化。我想使用 DataContractSerializer。如果我删除不需要的导航属性的 [DataMemberAttribute] 会起作用吗?

I've been reading about serialization of entities graph in an entity framework context using Linq to entities and the different possible serializers: Binary, XmlSerializer and DataContractSerializer.
As i understood the binary and XmlSerializer serialize the entity without its relationships. In case relationships are serialized it would cause a problem because of the nature of the resulting xml file structure ( for XmlSerializer).
The DataContractSerializer serialize the graph in its whole depth unless the lazy load is disabled.

My question is: I want to serialize a part of the graph. For example if I have an entity A and three related entities B, C and D, only B and D would be serialized with A. I want to use the the DataContractSerializer. If I delete the [DataMemberAttribute] of the unwanted navigational properties would that work?

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

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

发布评论

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

评论(3

无风消散 2024-12-06 18:17:29

您实际上可以禁用延迟加载、序列化/反序列化,然后重新启用延迟加载。

context.ContextOptions.LazyLoadingEnabled = false;

StackOverflow来源

You can actually disable lazy-loading, serialize/deserialize, and then re-enable lazy-loading.

context.ContextOptions.LazyLoadingEnabled = false;

StackOverflow Source

苹果你个爱泡泡 2024-12-06 18:17:29

由于属性是静态元数据,因此您不能在运行时弄乱它们。如果您将它们从您的实体中删除,它们将被永久删除。

延迟加载可能不是您想要的,因为当您加载整个图时,部分图场景通常仅在更新或部分插入时出现。

根据我收集的情况,您的情况是当您想要更新某些内容时,您只想更新部分图表,而不是客户端上的整个图表。实现此目的的一种方法是手动删除其他 DataMember,并将它们设置为 null,序列化它们,更新,然后修复您之前设置的 null 引用,最后确保 ChangeTracker 都处于之前的状态。

在我们的特定开发场景中,我们通过 T4 模板实现了此行为,该模板生成所有混乱的代码,生成“数据管理器”的一部分,用于处理客户端上存在的所有自跟踪实体。

Since attributes are static metadata you can't mess with them at run-time. And if you remove them from your entity they will be permanently removed.

The Lazy loading isn't probably what you want, since when you load you bring the entire graph, the partial graph cenario only usually comes up on updates or partial inserts.

Your cenario, from what I gathered is when you want to update something, you want to update a partial graph only, and not the entire graph that you have on the client. One way to achieve this is to remove manualy the other DataMembers and set them to null, serialize them, update, and them repair the null references you previously set, finally make sure the ChangeTrackers are all in their previous state.

In our specific development cenario, we achieved this behaviour through a T4 template, that generates all the messy code, generating part of a "DataManager" that handles all the Self Tracking Entities once they exist on the client.

你的他你的她 2024-12-06 18:17:29

根据我的经验,禁用延迟加载的唯一可靠方法似乎是转到实体设计器卷绕器,在后台右键单击,选择“属性”,然后在“属性”窗口中将“延迟加载已启用”设置为 false 。

In my experience, it seemed like the only reliable way to disable lazy-loading is to go to the Entity Designer winder, right-click in the background, select "Properties", and set "Lazy Loading Enabled" to false in the Properties window.

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