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()
. 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. 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.
Asked
Active
Viewed 91 times
1

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Kit
- 20,354
- 4
- 60
- 103
-
Please show the code used for attaching and adding entities. – Ladislav Mrnka Jan 11 '12 at 16:06
-
It's too complex at the moment. It's in a generic repository implementation. I'll try to scale it back. – Kit Jan 11 '12 at 16:10
1 Answers
0
The previously attached entities were identical structurally, but they were different references. EF seems to use ReferenceEquals
under the covers.

Kit
- 20,354
- 4
- 60
- 103