1

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)

Arbross
  • 45
  • 1
  • 9
  • What database are you using? – jdweng Jun 18 '22 at 20:35
  • I tried to use PostgreSql and MSSQL(now mssql) – Arbross Jun 18 '22 at 20:36
  • MSSQL is a multi-user database and should allow simultaneous queries. In some cases there hare to be locks to prevent two queries from changing data. For example if there is a object in the database that has a quantity of 10 one request removes 8 another request cannot remove 5. The locks are in the database and should not be giving the error you are getting. The error you are getting is usually for a database that is not designed for multi-user like an excel workbook. Excel uses either JET or ACE driver which is not capable of multi-user. MSSQL is multi-user. – jdweng Jun 19 '22 at 01:15
  • Okay, thank you i understand. And what i can do in my case? – Arbross Jun 19 '22 at 08:20
  • See : https://stackoverflow.com/questions/62253837/the-instance-of-entity-type-cannot-be-tracked-because-another-instance-with-the, and https://stackoverflow.com/questions/48202403/instance-of-entity-type-cannot-be-tracked-because-another-instance-with-same-key, and https://entityframeworkcore.com/knowledge-base/48117961/the-instance-of-entity-type-----cannot-be-tracked-because-another-instance-of-this-type-with-the-same-key-is-already-being-tracked – jdweng Jun 19 '22 at 09:43
  • Didn't help, still throws the same exception when updating via UserManager – Arbross Jun 20 '22 at 14:24
  • Did you check the log files in MSSQL? Using SSMS the logs are under Management. – jdweng Jun 20 '22 at 15:02
  • I don't use SSMS, i have logs in console from asp.net web api, there is no errors or warnings – Arbross Jun 20 '22 at 16:41

1 Answers1

0

Are you getting "user" twice? If you get a "user1" with id = 1 and "user2" with id = 1, you will have this problem.