Description
openedon Nov 16, 2021
Use State to set the state of only a single entity.
Am I right in thinking this no longer happens due to the cascade changes? It appears so in my code.
I have 2 related entities, one is read from the DB, the other is attached, updated, saved and then detached. What I find is that this removes both entities from the change tracker such that subsequent updates to the original DB-sourced entity do not get saved back to the DB on subsequent calls to SaveChanges..
I see changetracker.DebugView.LongView get cleared when calling dbEntry.State = EntityState.Detached;
on the object I set the State of, ie it clears the related entities that were already present. This means the docs are wrong - setting State adds the object to the change tracker and links it into the graph of related entities (whether directly or indirectly).
I think the docs were intended to be the correct behaviour.
Note that I add the entity to the change tracker by setting dbEntry.State = EntityState.Unchanged
and not using the ChangeTracker.Attach() method, as per the docs, deliberately to affect this 1 entity independantly.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 06501e5d-2617-9380-1a99-389a727b3012
- Version Independent ID: db1657b7-d390-7a27-062c-408e166a85ca
- Content: DbContext.Attach Method (Microsoft.EntityFrameworkCore)
- Content Source: dotnet/xml/Microsoft.EntityFrameworkCore/DbContext.xml
- Product: ef-core-api
- GitHub Login: @dotnet-bot
- Microsoft Alias: avickers