实体框架分离一个实体,相关实体消失

发布于 2024-12-20 21:30:58 字数 102 浏览 1 评论 0原文

当我使用实体框架时,我想查询上下文中的记录并将其添加到具有相同架构的另一个上下文中,查询出记录后,我将其与上下文分离,但相关实体都消失了,是吗?有什么办法解决吗?

提前致谢!

When I use Entity Framework, I want to query out a record in a context and add it to another context with the same schema, after query out the record, I detach it from the context, but the related entities are all away, is there any way to solve it?

Thanks in advance!

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

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

发布评论

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

评论(2

婴鹅 2024-12-27 21:30:58

这是“设计使然”。 EF 只能一一分离实体,但同时 EF 不支持由附加实体和分离实体组成的对象图。因此,当您分离实体时,它将破坏与附加对象图其余部分的所有关系。目前不支持分离整个对象图,但您可以在 数据 UserVoice

作为解决方法,您可以关闭上下文上的延迟加载,使用 @CodeWarrior 描述的预先加载来准确加载您需要传递到其他上下文的数据。加载数据后,将它们序列化为流式传输,并立即将它们反序列化为对象图的新实例。这是如何对实体图进行深度克隆的方法,该实体图是分离的,但所有关系都完好无损(需要关闭延迟加载,否则序列化也会加载所有其他导航属性,这可能会导致比预期大得多的对象图)。唯一的要求是您的实体必须可以由您选择的序列化器进行序列化(请注意循环引用,这通常需要对实体进行一些特殊处理或附加属性)。

This is "by design". EF can detach entities only one by one but in the same time EF doesn't support object graphs composed of attached and detached entities. Because of that when you detach entity it will break all relations to the rest of attached object graph. Detaching whole object graph is currently not supported but you can vote for this feature on Data UserVoice.

As a workaround you can turn off lazy loading on your context, use eager loading described by @CodeWarrior to load exactly data you need to pass to other context. Once you have data loaded serialize them to stream and immediately deserialize them to the new instance of the object graph. This is the way how to make deep clone of entity graph which is detached but has all relations intact (turning lazy loading off is needed otherwise serialization will load all other navigation properties as well which can result in much bigger object graph then expected). The only requirement is that your entities must be serializable by serializer of your choice (be aware of circular references which usually require some special handling or additional attributes on your entities).

徒留西风 2024-12-27 21:30:58

您是问如何加载子实体吗?如果是这样,您可以使用 .Include 方法进行预先加载。给定一个 Person 类和一个 PhoneNumber 类,其中 Person 具有 PhoneNumber 的集合,您可以执行以下操作:

List<Person> People = db.People.Where(p => p.Name = "Henry")
                               .Include("PhoneNumbers")
                               .ToList();

或者您可以执行所谓的显式加载,在其中加载实体并对子实体和相关实体的集合调用 .Load 方法您想要加载的内容。通常,当您没有启用 LazyLoading 时,您会执行此操作(并且 LazyLoading 在 4.0+ 中默认启用,不记得在以前的版本中)。

无论您如何查询和加载它们,您都必须分离要附加到不同上下文的实体。

这是一篇非常好的 MSDN 有关加载实体的文章

Are you asking how to load the child entities? If so, you can do eager loading with the .Include method. Given a Person class and a PhoneNumber class where Person has a collection of PhoneNumber, you could do the following:

List<Person> People = db.People.Where(p => p.Name = "Henry")
                               .Include("PhoneNumbers")
                               .ToList();

Or you can do what is called explicit loading where you load your entities and call the .Load method on the collections of child and related entities that you want to load. Generally you do this when you do not have LazyLoading enabled (and LazyLoading is enabled by default in 4.0+ don't recall in previous versions).

Regardless of how you query and load them, you will have to detach entities that you want to attach to a different context.

Here is a link to a pretty good MSDN article on loading entities.

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