The instance of entity type 'IdentityUserLogin<string>' cannot be tracked because another instance with the same key value for {'LoginProvider', 'ProviderKey'} is already being tracked


Questions: How do you solve: The instance of entity type 'IdentityUserLogin<string>' cannot be tracked because another instance with the same key value for {'LoginProvider', 'ProviderKey'} 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.


Solution: This means that the external Login Provider has stored a Key Pair Value that you currently have in your database. For Example user is trying to log in with Google and the User had logged into your application at some point and the application stored the Key related to the user. The Application would then throw an error that the Key is already in the database.

- Best way to resolve this issue is by finding a Kay related to the User and then modifying it or delete it.

1. First get the ProverKey in your code
var providerKey = info.ProviderKey.ToString()
2. When you get the providerKey when debugging, go to your LoginProvider User Table and search for that providerKey
3. Remove it or modify it so it does not become duplicate



Another way to resolve this error when you are working with another Entity, or code base not related the IdentityUserLogin Model as discussed above: Follow the solution before.

1. The reason why the error "The instance of entity type 'EntityTypeInHere' cannot be tracked because another instance with the same key value for 'KeyType' is already being tracked" is because you enabled the ChangeTracking mechanisim in your application. Entity Framework automatically Tracks any changes done to the Entities. This slows the Application Performance at times, but according to the Team at Microsoft, it was mentioned that if you don't need to perform any operations on the your Entites then it is safe to say disable tracking. 

The way you disable tracking is by simply _context.ChangeTracker.AutoDetectChangesEnabled = false.

I
like to disable tracking in the specific function that caused the error to be thrown, otherwise, for the application wide state, I like the ChangeTracker to be enabled.

[Note]: If you are just retrieving the data from the database and do not need any tracking enabled on the Entities, then include
AsNoTacking() in your EF Core Query. However, it is a bad practice if you think you won't perform any operations on the Entities retrieved from the database and suddenly you are doing some edits to the Data and commiting/saving them to the Database.

The team pointed out the the Operation it takes to enabled ChangeTracker to when the Entity was retrieved by AsNoTracking() then do operation on the Entity is very expesive than just leaving the ChangeTracker Enabled throughout the Application Life Cycle.



Another solution worth looking into is that, there is a chance that you are using _context.Update(EntityNameInHere); when all your Entities retrieved from the database are getting tracked.

If this is the case then do not do _context.Update(EntityNameHere); instead just SaveChanges();

Since the Entity Framework ChangeTracker is already tracking the Changes to the Entities you don't need to call Update() function on the Context instead just SaveChanges().

- If _context.SaveChanges() does not work, try explicitly specifying what Entity DbSet<EntityType> are you trying to update. Type _context.MyEntity.Update(newObjectOrUpdatedObject);


I hope this helps. 







For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]