Open yonidagan opened 2 years ago
Note for triage: The context should be considered in an invalid state after an InvalidOperationException
has been thrown--https://github.com/dotnet/EntityFramework.Docs/issues/3401 tracks documenting this better. We should discuss if this also applies to DbUpdateException
. (Note that it does not apply to DbUpdateConcurrencyException
.)
Note from triage: DbUpdateException should not leave the context in a bad state.
Hi @AndriySvyryd - any chance this can be fixed soon?
@sagilevanon This issue is in the Backlog milestone. This means that it is not planned for the next release. We will re-assess the backlog following the this release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.
EF is wrongly adding (persisting) an entity when a duplicate key is found.
Consider the following Entity, when Name is set as Alternate Key:
Then consider the following scenario:
Exception on step 3 is:
The following code simulates that process:
It seems the DbSet.Local is wrongly updated with the entity even though an exception was thrown during the call to .Add A full console app sample project to reproduce this issue is attached.
EF Core version: 6.0.8 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 EFDuplicateKey.zip