I know about questions has already been asked, but solutions didn't help me.
The instance of entity type 'User' 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 use Identity update method and it is invoke this exception.
await _userManager.UpdateAsync(user);
Also I tried to use Repository pattern update and it is updating right.
public async Task UpdateAsync(TEntity entity)
{
_dbSet.Update(entity);
_context.Entry(entity).State = EntityState.Modified;
}
By using attach it isn't updating connected to user class(by navigation property).
public async Task UpdateAsync(TEntity entity)
{
_dbSet.Attach(entity);
_context.Entry(entity).State = EntityState.Modified;
}
What am i doing wrong? I think a class of Identity should work correctly.
UPDATE : The solution was that I tried to get linked tables from the database to the main one (eg Categories to Users) and work with them (which is not the right approach). You should follow the principle of getting only what you need at the moment (if you need to update a category, you don't need to do it through a connected user). That is, to receive from the database only what will be processed. Otherwise, it will lead to similar errors. Specifically, this one also consisted in the fact that my DTOs were not configured in the same way and stored instances of the same user there, which caused a conflict.
Wrong DTO :
class ExampleCategoryDTO
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IList<User> Users { get; set; } --- wrong
}
Wrong work with data :
var tmp = context.Include(x => x.Categories).Users.ToList();
// Imagine we're changing some "tmp" variable data in categories
context.Update(tmp);
context.SaveChanges();
Thanks to those who tried to help and sorry for the lack of information (I didn't really understand where the problem was coming from)