add entity The instance of entity type ‘xxxx‘ cannot be tracked because another instance with the sa

出现的错误:
The instance of entity type ‘xxxx’ 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

实体类型’ xxxx’的实例不能被跟踪,因为另一个具有相同键值的{‘Id’}的实例已经被跟踪了。在附加现有实体时,确保只附加一个具有给定键值的实体实例。考虑使用’DbContextOptionsBuilder '。启用ensitivedatalogging '以查看冲突的键值。

解决方法:
我用的是 Scaffold-DbContext 生成的实体,当数据表没有把主键设为自增时,用Scaffold-DbContext 生成的实体里会自动加上 ValueGeneratedNever 的属性。在insert 多个实全时会出现该情况。只要把数据表的主键设为自增再重新 Scaffold-DbContext 后 ValueGeneratedNever 就会被去除掉。这时就不会出现上面的错误了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值