question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core instance of entity type cannot be tracked

See original GitHub issue

I’ve been following the approaches proposed in this repo and the associated pluralsight course, and they have worked really well, but recently been getting the following exception thrown by EFCore:

“The instance of entity type ‘YourEntity’ cannot be tracked because another instance with the same key value for {‘Id’} 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.”

I have created a minimum example to demonstrate the issue (using EFCore inmemory and a Sqlite DB) here: https://github.com/robertlarkins/efcore-cannot-track-entity-issue

As far as I can determine it is related to static entities (such as Course in this repo, or Appointment Status in my linked repo), and it occurs when the context is holding an instance of AppointmentStatus pulled from the database, and the same AppointmentStatus is added to the context from the static AppointmentStatus reference in code. When trying to save the context to the database the above exception gets thrown.

This exception occurs on this line:

IEnumerable<EntityEntry> enumerationEntries = ChangeTracker.Entries()
                .Where(x => EnumerationTypes.Contains(x.Entity.GetType()));

so it never gets to the point of trying to set the EntityState as unchanged.

Is this an issue you have come across before? I couldn’t find how this repo stops this occurring, hopefully I have just missed something, but if not, then I assume it could potentially happen within this repo as well. Alternatively should this issue be presented on the EFCore issue tracker?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vkhorikovcommented, May 14, 2021

Not sure why this is not working. Try adding lazy loading to your reference project, maybe that’s the difference here.

It’s impossible to make changes in CSharpFunctionalExtensions to avoid this issue, it is core to how EF keeps track of entities in its DbContext. As usual, it’s unnecessarily overcomplicated; it’s a solved problem in NHibernate.

0reactions
pantoniscommented, Oct 12, 2022

FYI I opened a ticket for this here

Read more comments on GitHub >

github_iconTop Results From Across the Web

The instance of entity type cannot be tracked because ...
I had this problem myself. Entity Framework keeps track of every object you insert into the database. So when you insert a duplicate...
Read more >
The instance of entity type cannot be tracked because ...
A variable in which entity instance is stored going out of scope in C# has nothing to do with DbContext and change tracking....
Read more >
Error In Update Record (The instance of entity type 'x' ...
The instance of entity type 'ApplicationRole' cannot be tracked because another instance with the same key value for {'Id'} is already being ......
Read more >
Entity Framework Error - Entity Type Cannot be Tracked
InvalidOperationException : 'The instance of entity type 'SomeData' cannot be tracked because another instance with the same key value for ...
Read more >
instance of entity type cannot be tracked because ... - YouTube
instance of entity type cannot be tracked because another instance with same key value is tracked - EF Core.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found