从上下文中分离对象并保持关系(导航属性)的最简单方法 [英] The easiest way to detach objects from context and keep relationships (navigation properties)

查看:62
本文介绍了从上下文中分离对象并保持关系(导航属性)的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当EF对象与上下文分离时,将删除所有关系(清除所有导航属性)。但是可以恢复导航属性的值。对于"1"中的每个NavigationProperties。 end(对象的简单链接)存在类型为"EntityReference< MyType>"的相关属性。此属性包含相关对象的类型名称和ID。从而可以遍历所有分离的对象并恢复所有关系。之后所有导航属性都将有效。
但我怀疑这是最简单有效的方法。还有其他方法吗?这是什么方式?
据我了解,这些关系是故意清除的,但我无法理解它的原因。是否有可能在不清除分离对象的关系的情况下调整上下文?

   When EF-objects are detached from the context, all the relationships are deleted (all navigation properties are cleared). But it is possible to restore values of navigation properties. For each NavigationProperties in "1" end (simple link to the object) exist related property of type "EntityReference<MyType>". And this property contains the type name and id for the related object. And thereby it is possible to iterate through all the detached objects and restore all relationships. And after that all the navigation properties will be valid.
   But I doubt that it is the easiest and valid way to do it. Do other ways exist? What are this ways?
  
As I understand, the relationships are cleared intentionally, but I can't understand the reason of it. Is it possible to tune context in way, where relationships for the detached objects are not cleared?

推荐答案

一个原因是垃圾收集。如果您分离实体,则不希望该实体内部的引用存在于由上下文管理的某个其他实体中,因为在分配实体和范围之外,GC都不能收集分离实体和上下文。

Well one reason is garbage collection.  If you Detach an entity you don't want a reference inside that entity to some other entity managed by the context to exist, because then neither your detached entity nor your context can be collected by the GC until both are disposed and out of scope. 

 

我理解你的痛苦,这让我有几个问题

I understand your pain, and it leads me to a couple questions

1。 yoiu是否需要完整的引用实体,或者您只需要保留密钥?

1.  Do yoiu want the full referenced Entity or do you just need to keep the key?

2。你分离的目的是什么?也许我们可以回避它。

2.  What is your purpose for Detaching?  maybe we can sidestep it. 


这篇关于从上下文中分离对象并保持关系(导航属性)的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆