简体   繁体   English

无法跟踪实体类型“用户”的实例,因为已经在跟踪另一个具有相同键值的“Id”实例

[英]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.

相关问题 无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例 - The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型 Model 的实例,因为已跟踪另一个具有相同 {&#39;Id&#39;} 键值的实例 - The instance of entity type Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“Bus”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例 - The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked 无法跟踪实体类型“AppUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“IdentityUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'IdentityUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“书签”的实例,因为已经在跟踪具有相同键值 {'ID'} 的另一个实例 - The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked 无法跟踪实体类型“Item”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型的实例,因为已跟踪另一个具有与 {'Id'} 相同键值对的实例 - The instance of the entity type cannot be tracked because another instance with the same key value pair for{'Id'} is already being tracked 无法跟踪实体类型“ Article”的实例,因为已经跟踪了另一个具有相同键值的{&#39;Id&#39;}实例 - The instance of entity type 'Article' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM