请选择 进入手机版 | 继续访问电脑版

大润晟泽博客

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 798|回复: 0

efcore报错误The instance of entity type ‘xxx‘ cannot be tracked

[复制链接]

16万

主题

16万

帖子

49万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
493071
发表于 2021-10-15 16:54:24 | 显示全部楼层 |阅读模式
asp.net core 3.1 环境:

场景: 一起执行几个方法体, 先在一个地方修改or添加一条记录;接着又在另个地方修改了该条记录,就会报如下错误:

  1. 具体详细错误信息:The instance of entity type 'xxx' 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.
复制代码
代码片段:

  1. foreach (var item in list)
  2.                 {
  3.                     var func = this.GetCodeCheckFunction(item);
  4.                     var gauge = UnitWork.FindSingle(func);

  5.                     if (gauge == null)
  6.                     {
  7.                         //新增
  8.                         UnitWork.Add(item);
  9.                     }
  10.                     else
  11.                     {
  12.                         //修改
  13.                         gauge.GaugeType = item.GaugeType;
  14.                         gauge.ModifiedBy = _auth.GetCurrentUser().User.Id;
  15.                         gauge.ModifiedTime = DateTime.Now;
  16.                         UnitWork.Update(gauge);
  17.                     }
  18.                     UnitWork.Save();
  19.                 }
复制代码

介绍下业务场景:导入时excel中存在重复数据。更新数据用的UnitWork。

上面的报错信息很明显了,它告诉我们一条数据已经被trace了,不能重复trace,只要我们把这条被trace的数据取消就行了。

下面是修改后的代码:
  1. foreach (var item in list)
  2.                 {
  3.                     var func = this.GetCodeCheckFunction(item);
  4.                     var gauge = _dbContext.MesInfoGauges.FirstOrDefault(func);

  5.                     if (gauge == null)
  6.                     {
  7.                         //新增
  8.                         _dbContext.MesInfoGauges.Add(item);
  9.                         _dbContext.SaveChanges();
  10.                         _dbContext.Entry(item).State = EntityState.Detached;
  11.                     }
  12.                     else
  13.                     {
  14.                         //修改
  15.                         gauge.GaugeType = item.GaugeType;
  16.                         gauge.ModifiedBy = user.Id;
  17.                         gauge.ModifiedTime = DateTime.Now;
  18.                         _dbContext.MesInfoGauges.Update(gauge);
  19.                         _dbContext.SaveChanges();
  20.                         _dbContext.Entry(entity: gauge).State = EntityState.Detached;
  21.                     }
  22.                 }
复制代码

保存后,修改当前数据的Entry状态为Detached。

若为.netcore 2.2,则写法为:

  1. _dbContext.Entry(gauge).State = EntityState.Detached;
复制代码

接着又报错:

  1. 报错内容:The property ‘ID’ on entity type ‘Equipment’ has a temporary value while attempting to change the entity’s state to ‘Modified’. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
复制代码

原因:表单中少了ID

解决方法:表单中加入







上一篇:EFcore mysql dbfirst 命令
下一篇:supervisor开机自启
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|大润晟泽博客 ( 鲁ICP备17022854号-3 )

GMT+8, 2024-3-29 15:55 , Processed in 0.081503 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表