一、EFCore 使用异常
The instance of entity type 'Model' 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.
The instance of entity type 'Model' 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.
无法跟踪实体类型“Model”的实例,因为已经在跟踪具有相同键值{'Id'}的另一个实例。在附加现有实体时,请确保只附加一个具有给定键值的实体实例。考虑使用' dbcontexttoptionsbuilder。EnableSensitiveDataLogging'查看冲突的键值。
错误分析:
从EF Core 6.0开始,这种类似的错误几乎很少出现;此次出现问题,也是针对部分用户出现,并非所有情况都出现异常,
出现场景:
进入列表,查询Include 关联表数据
点击处理,还是处理 关联表数据,然后删除时候抛出异常。
二、解决方案
在使用EF Core 的时候,当前执行代码读取 关联表数据,然后删除关联表数据,使用同一个上下文操作。务必使用同一个上下文对象。
跟多: