简体   繁体   English

为什么我将引用的实体添加到上下文而不是附加的上下文中,保持不变?

[英]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.

相关问题 DbContext破坏附加的实体:为什么? - DbContext corrupts attached entities: why? 为什么我只得到“ .xlsx”而不是完整的串联字符串 - Why do I get only “.xlsx” instead of full concatenated string 未使用(和引用)的程序集上的程序集引用错误,为什么会出现此错误? - Assembly reference error on not used (and referenced) assembly, why do I this error? EF Core 上下文不包含添加实体的更改 - EF Core Context does not contain changes for added entities 如何在不依赖公共数据上下文实例的情况下维护相关实体之间的引用透明度? - How do I maintain referential transparency between related entities without relying on a common data context instance? 为什么我得到正方形而不是文字? - Why I get squares instead of text? 查询附加的实体 - query the attached entities 如何保持 IQueryable 的优势<t>在使用 ViewModel 或 DTO 而不是 ORM 生成的实体时?</t> - How do I keep the Advantages of IQueryable<T> while using ViewModels or DTOs instead of ORM generated Entities? 如何使用数字字符实体而不是问号将XmlDocument.Save()编码为“ us-ascii”? - How do I XmlDocument.Save() to encoding=“us-ascii” with numeric character entities instead of question marks? 如何检查附加到 EF 上下文的 POCO 的状态? - How do you check the state of a POCO that is attached to the EF Context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM