diff --git a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs index 50d8a086e81..fb1434aa33e 100644 --- a/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs +++ b/src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs @@ -176,7 +176,7 @@ public virtual int ExecuteTransaction( continue; } - table.Delete(entry, updateLogger); + table.Delete(entry.SharedIdentityEntry, updateLogger); } switch (entry.EntityState) diff --git a/test/EFCore.Specification.Tests/TestModels/UpdatesModel/Rodney.cs b/test/EFCore.Specification.Tests/TestModels/UpdatesModel/Rodney.cs new file mode 100644 index 00000000000..1d1c1d13873 --- /dev/null +++ b/test/EFCore.Specification.Tests/TestModels/UpdatesModel/Rodney.cs @@ -0,0 +1,16 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +using System.ComponentModel.DataAnnotations; + +namespace Microsoft.EntityFrameworkCore.TestModels.UpdatesModel; + +public class Rodney +{ + public string Id { get; set; } = null!; + + [ConcurrencyCheck] + public DateTime Concurrency { get; set; } +} diff --git a/test/EFCore.Specification.Tests/TestModels/UpdatesModel/UpdatesContext.cs b/test/EFCore.Specification.Tests/TestModels/UpdatesModel/UpdatesContext.cs index 81861b9625d..b372ea9aecb 100644 --- a/test/EFCore.Specification.Tests/TestModels/UpdatesModel/UpdatesContext.cs +++ b/test/EFCore.Specification.Tests/TestModels/UpdatesModel/UpdatesContext.cs @@ -14,6 +14,7 @@ public class UpdatesContext : PoolableDbContext public DbSet ProductView { get; set; } = null!; public DbSet ProductTable { get; set; } = null!; public DbSet ProductTableView { get; set; } = null!; + public DbSet Trotters { get; set; } = null!; public UpdatesContext(DbContextOptions options) : base(options) diff --git a/test/EFCore.Specification.Tests/UpdatesTestBase.cs b/test/EFCore.Specification.Tests/UpdatesTestBase.cs index 85263a07565..7d3d14d07c7 100644 --- a/test/EFCore.Specification.Tests/UpdatesTestBase.cs +++ b/test/EFCore.Specification.Tests/UpdatesTestBase.cs @@ -25,6 +25,45 @@ protected UpdatesTestBase(TFixture fixture) new object[] { false } }; + [ConditionalTheory] // Issue #25905 + [InlineData(false)] + [InlineData(true)] + public virtual async Task Can_delete_and_add_for_same_key(bool async) + => await ExecuteWithStrategyInTransactionAsync( + async context => + { + var rodney1 = new Rodney { Id = "SnotAndMarmite", Concurrency = new DateTime(1973, 9, 3) }; + if (async) + { + await context.AddAsync(rodney1); + await context.SaveChangesAsync(); + } + else + { + context.Add(rodney1); + context.SaveChanges(); + } + + context.Remove(rodney1); + + var rodney2 = new Rodney { Id = "SnotAndMarmite", Concurrency = new DateTime(1973, 9, 4) }; + if (async) + { + await context.AddAsync(rodney2); + await context.SaveChangesAsync(); + } + else + { + context.Add(rodney2); + context.SaveChanges(); + } + + Assert.Equal(1, context.ChangeTracker.Entries().Count()); + Assert.Equal(EntityState.Unchanged, context.Entry(rodney2).State); + Assert.Equal(EntityState.Detached, context.Entry(rodney1).State); + + }); + [ConditionalFact] public virtual void Mutation_of_tracked_values_does_not_mutate_values_in_store() {