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

错误分析:

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

出现场景:

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

 

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

 

 

二、解决方案

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

 

跟多:

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值