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

Entity update fails when using update record #24001

Closed
jtleaming opened this issue Jan 27, 2021 · 1 comment
Closed

Entity update fails when using update record #24001

jtleaming opened this issue Jan 27, 2021 · 1 comment

Comments

@jtleaming
Copy link

###Background
Record was previously a class and I changed it to a record. For my test to work I had to use the with syntax when calling the UpdatedRecord method. This failed in my test and also when trying this against the running application using SqlServer.

I found this line in IdentityMap.cs which ends up throwing the exception...

269   var bothStatesEquivalent = (entry.EntityState == EntityState.Deleted) == (existingEntry.EntityState == EntityState.Deleted);

Code

public class RequestDbRepository 
    {
        private readonly DbSet<Request> requests;
        private readonly Contexts contexts;

        public DbRepository(Contexts contexts)
        {
            this.contexts = contexts;
            requests = contexts.Requests!;
        }
        public bool UpdateRequest(Request request)
        {
            requests.Update(request);
            contexts.SaveChanges();
            return true;
        }
    }

public record Request(string Status)
{
     public string Status {get; init;}
}

public class Test {
        [Fact]
        public void UpdateRequest_Should_Update()
        {

            Request request = new Request("Pending");

            subject.SaveRequest(request);

            var result = subject.UpdateRequest(request with
            {
                Status = "Approved"
            });

            var actualRequest = subject.GetRequest(request.RequestId);

            result.Should().BeTrue();
            request.Should().Be(actualRequest);
        }
}

Stack trace

System.InvalidOperationException
The instance of entity type 'TimeOffRequest' cannot be tracked because another instance with the same key value for {'Key'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.ThrowIdentityConflict(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry, Boolean updateDuplicate)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(TKey key, InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.SetEntityState(InternalEntityEntry entry, EntityState entityState)
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Update(TEntity entity)

provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer 5.0.0
Test Database provider: Microsoft.EntityFrameworkCore.Sqlite 5.0.0
Target framework: .NET 5.0
Operating system: Windows 10/WSL Ubuntu
IDE: Rider

@smitpatel
Copy link
Member

Duplicate of #11457

@smitpatel smitpatel marked this as a duplicate of #11457 Jan 27, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants