[英]Why do I get referenced entities added to context instead of attached, unchanged?
I have attached entities to a context with EntityState.Unchanged
which will be referenced by a new entity I add later via DbContext<T>.Add()
. 我已将实体附加到具有
EntityState.Unchanged
的上下文,该上下文将由稍后通过DbContext<T>.Add()
添加的新实体引用。 The Add()
adds the entity, but it also adds the other entities it references to the added collection in ObjectStateManager
, despite the referenced ones being in the unchanged collection previously. Add()
添加实体,但也会将它引用的其他实体添加到ObjectStateManager
的已添加集合中,尽管之前引用的实体位于未更改的集合中。 Under the debugger I see the referenced entities in both collections. 在调试器下,我在两个集合中都看到了引用的实体。 So, I end up with inserts occurring on the referenced children entities when I don't want them to.
因此,当我不希望插入子实体时,我最终会在这些子实体上发生插入。
The previously attached entities were identical structurally, but they were different references. 先前连接的实体在结构上相同,但是它们是不同的参考。 EF seems to use
ReferenceEquals
under the covers. EF似乎在幕后使用
ReferenceEquals
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.