[英]The instance of entity type 'User' cannot be tracked because another instance with the same key value for 'Id' is already being tracked
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.
无法跟踪实体类型“用户”的实例,因为已经在跟踪另一个具有与 {'Id'} 键值相同的实例。 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.
考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。
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.
我认为身份的 class 应该可以正常工作。
Are you getting "user" twice?您是否两次获得“用户”? If you get a "user1" with id = 1 and "user2" with id = 1, you will have this problem.
如果你得到一个 id = 1 的“user1”和一个 id = 1 的“user2”,你就会遇到这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.