The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val

一、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'查看冲突的键值。

The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val_第1张图片

错误分析:

从EF Core 6.0开始,这种类似的错误几乎很少出现;此次出现问题,也是针对部分用户出现,并非所有情况都出现异常,

出现场景:

进入列表,查询Include 关联表数据

The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val_第2张图片

 

点击处理,还是处理 关联表数据,然后删除时候抛出异常。

The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val_第3张图片

The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val_第4张图片 

 

二、解决方案

在使用EF Core 的时候,当前执行代码读取 关联表数据,然后删除关联表数据,使用同一个上下文操作。务必使用同一个上下文对象。

The instance of entity type ‘Model‘ cannot be tracked because another instance with the same key val_第5张图片

 

跟多:

EF 框架的简介、发展历史;ORM框架概念

ef core 读取text类型慢_ef core读取大字符串字段慢

EFCore 关联表查询_多表使用整理

 

你可能感兴趣的:(EntiryFramework,前端,javascript,C#,EF,Core)