Open Daniel-Svensson opened 7 months ago
The problematic code seems to be with the "_dependentMaps" and StateManager
WHen "UnTrracking" an entity it seems like StateManager tries to remove it from from DependentMap .
Entity is NOT removed from dependent maps, which seems somewhat due to the TryCreateFromCurrentValues(entry, out var key) in DependentMap.Remove returning false (due to the Id property having been flagged as null). So the entry stays in the internal cache in stead of being removed on Detach.
The entry for "Id = -7" is still left in the map and not removed when it calls map.Remove()
with an entry that has "Id = -7"
Note for team: confirmed bug when iterating over tracked entities and detaching them. Note that this is typically anti-pattern. It is being done here because some other state is being maintained in the context between units of work.
When is this bug expected to be fixed? In version 8 or version 9?
After having detached entities from the DbContext in EF Core 8 and made sure that they are no longer part of
ChangeTracker.Entries()
they will still automatically be added back later if a later object has a FK or similar with their Id.If attaching an entity with id X, detaching it and then later attaching it a InvalidOperationException will be thrown. If you on the other hand add a dependant/dependent on X a navigation fixup might happen against an instance which is not really part of the DbContext nor the Database...
The following problem is new in EF Core 8, it works fine with earlier version.
The behaviour was triggered by some code which generates temporary entities, attaches them to a DbContext (so that fixup of navigation properties) happens, runs some code and then detaches them (there is still some cached data in the context used for all runs). The temporary entities must sometimes reuse the same Id
Include your code
I expect the following code to work the same way as for EF Core 7, where the new object graph is attached without any problem.
Include stack traces
Include the full exception message and stack trace for any exception you encounter.
Use triple-tick fences for stack traces. For example:
Include provider and version information
EF Core version: 8.0.4 Database provider: * Target framework: .NET 8.0 Operating system: windows 11 IDE: N/A