Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InMemory provider deletes wrong entry when DELETE and ADD for same PK #25905

Closed
dtkujawski opened this issue Sep 7, 2021 · 0 comments · Fixed by #28979
Closed

InMemory provider deletes wrong entry when DELETE and ADD for same PK #25905

dtkujawski opened this issue Sep 7, 2021 · 0 comments · Fixed by #28979
Labels
area-in-memory area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@dtkujawski
Copy link

dtkujawski commented Sep 7, 2021

Description

When using the InMemory provider, DELETING and ADDING entities with the same PK in the same SAVE operation will cause a concurrency violation (if concurrency is turned on). This is happening because the InMemoryStore class is deleting the wrong entity.

See fork for fix and unit test code: https://github.com/dtkujawski/efcore/tree/InMemorySharedEntry
Commit: dtkujawski@9e9c7f3

Example Code

  //seed context with a single record         
  context.Add(new Record { Key = "KEY", Concurrency = new DateTime(2021, 1, 1, 1, 1, 1) });
  context.SaveChanges();

  //mark existing record as "deleted" in change tracker
  context.Records.RemoveRange(context.Records);

  //insert new record with same key as being deleted
  context.Add(new Record { Key = "KEY", Concurrency = new DateTime(2021, 2, 2, 2, 2, 2) });

  //save (should delete 1st record and add second record) - throws Concurrency exception
  context.SaveChanges();

Root cause

I believe the issue is here:

if (entry.SharedIdentityEntry != null)
{
if (entry.EntityState == EntityState.Deleted)
{
continue;
}
table.Delete(entry);
}

Instead of table.Delete(entry); I believe it should be table.Delete(entry.SharedIdentityEntry);

Stack traces

Message: 
    Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException : Conflicts were detected for instance of entity type 'Record' on the concurrency token properties {'Concurrency'}. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting values.
  Stack Trace: 
    InMemoryTable`1.ThrowUpdateConcurrencyException(IUpdateEntry entry, Dictionary`2 concurrencyConflicts) line 380
    InMemoryTable`1.Delete(IUpdateEntry entry) line 218
    InMemoryStore.ExecuteTransaction(IList`1 entries, IDiagnosticsLogger`1 updateLogger) line 183
    InMemoryDatabase.SaveChanges(IList`1 entries) line 77
    StateManager.SaveChanges(IList`1 entriesToSave) line 1101
    StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess) line 1151
    NonRetryingExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) line 49
    StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) line 1131
    DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) line 507
    DbContext.SaveChanges() line 468
    SharedIdentityEntryTest.Shared_identity_entry_delete_and_insert_with_concurrency() line 35

Version information

EF Core version: 5.0
Database provider: InMemory
Target framework: 5.0.9
Operating system: Windows
IDE: Visual Studio 2019 16.9.6

@ajcvickers ajcvickers added this to the Backlog milestone Sep 11, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 10, 2021
@ajcvickers ajcvickers self-assigned this Sep 2, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 5, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 9, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-in-memory area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants