这里记录一个在使用.net core中ef core执行数据库操作时遇到的问题:

我在代码中使用DbContext下的Update方法准备将更改后的数据像这样步到数据库:

_context.Menus.Update(menu);

这是很常见的用法,但没想到一直报如下错误:

The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.

使用谷歌翻译翻译为:

无法跟踪实体类型“Menus”的实例,因为已经跟踪了具有相同键值的{'Id'}的另一个实例。

我的代码如下:

 if (!menu.OrderNumber.HasValue)
{
var maxOrderItem =
_context.Menus.Where(x => x.ParentId == menu.ParentId)
.OrderByDescending(x => x.OrderNumber)
.FirstOrDefault();
menu.OrderNumber = maxOrderItem != null ? maxOrderItem.OrderNumber + : ;
}
///EF core中没有AddOrUpdate方法,所以针对是新增菜单还是修改菜单做出判断
if (isNewMenu)
{
_context.Menus.Add(menu);
}
else
{
_context.Menus.Update(menu);///此处报出上述异常
}
_context.SaveChanges();

我通过百度,发现国内网站上没有出现类似错误的记载,最后,我在stackoverflow上看到一段类似问题描述:

该用户描述,他进行了如下尝试:

在使用_context获取值时,使用AsNoTracking()方法,我进行尝试,修改我的代码如下:

if (!menu.OrderNumber.HasValue)
{
var maxOrderItem =
_context.Menus.AsNoTracking().Where(x => x.ParentId == menu.ParentId)
.OrderByDescending(x => x.OrderNumber)
.FirstOrDefault();
menu.OrderNumber = maxOrderItem != null ? maxOrderItem.OrderNumber + : ;
}
///EF core中没有AddOrUpdate方法,所以针对是新增菜单还是修改菜单做出判断
if (isNewMenu)
{
_context.Menus.Add(menu);
}
else
{
_context.Menus.Update(menu);
}
_context.SaveChanges();

发现我遇到的这个问题得以圆满解决。

我在这里记录一下这个问题,但其中的道理我没有深究,就不说了。

The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.的更多相关文章

  1. The instance of entity type 'xxxx' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.

    一.问题描述 问题:The instance of entity type 'xxxx' cannot be tracked because another instance with the sam ...

  2. EF 更新实体 The instance of entity type 'BabyEvent' cannot be tracked because another instance

    加上AsNoTracking. 人不能两次踏入同一条河. 我 就踏入了.o(╥﹏╥)o

  3. The instance of entity type 'manager' 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 ent

    最近在用ASP.NET CORE时遇到一些问题,现记录下: 出现上述错误,即在更新实体数据时出现的错误 services.AddDbContext<StoreContext>(c => ...

  4. [转] EF cannot be tracked because another instance of this type with the same key is already being tracked

    本文转自:http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-ob ...

  5. type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object

    今天在进行代码检查的时候出现下面的异常: type parameters of <T>T cannot be determined; no unique maximal instance ...

  6. The entity type XXX is not part of the model for the current context.

    今天遇到了一个奇葩问题,虽然解决了,但还是一脸懵,先附赠一下别人的解决方案:https://www.cnblogs.com/zwjaaron/archive/2012/06/08/2541430.ht ...

  7. Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.

    背景 有一个 Flask 项目,然后有一个路由返回的是 dict 通过浏览器访问,结果报错 关键报错信息 TypeError: 'dict' object is not callable The vi ...

  8. Entity Framework 6如何进行导航属性的筛选(context.Msg.First(t=>t.Id==1).Include(t=>t.MsgDetail),筛选MsgDetail带条件)

    问题: https://q.cnblogs.com/q/98333/ Msg表(Id,Content,IsDel).内有 virtual ICollection<MsgDetail> Ms ...

  9. The entity type <type> is not part of the model for the current context

    这是在网站里遇到的一个错误,自动生成的不能手动添加, reference: http://stackoverflow.com/questions/19695545/the-entity-type-xx ...

随机推荐

  1. (原)在anaconda3+ubuntu16.04中编译Pose flow中的deepmatching动态库

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11285239.html 参考网址: https://github.com/YuliangXiu/Po ...

  2. ROS官网新手级教程总结

    第 1 关卡:安装和配置 ROS 环境 目标:在计算机上安装和配置 ROS 环境. 安装 ROS 按照 ROS 安装说明进行安装. 管理环境 确定环境变量 ROS_ROOT 和 ROS_PACKAGE ...

  3. Latex使用过程中的一些总结

    本文主要总结在使用Latex过程中遇到的一些问题及解决方案. 一:关于参考文献 1.如何在paper同一处用\cite命令同时引用多篇文献? 用\cite{bibtex1}\cite{bibtex2} ...

  4. reactnative遇到的问题总结

    1.View中出现文本报错,View等标签中不能出现字符串文本,字符串文本需要包在Text中,遇到如下错误 下面是问题代码: let rightTitle = this.props.rightTitl ...

  5. application platform as a service (aPaaS)

    Application platform as a service (aPaaS) is a cloud service that provides environments for the deve ...

  6. NOIP 2003 栈

    洛谷 P1044 栈 洛谷传送门 JDOJ 1291: [NOIP2003]栈 T3 JDOJ传送门 题目描述 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两 ...

  7. 前端 / JavaScript 导出PDF的实践

    1.库:jspdf,自己定义一个高宽,如A4的210mm×297mm 2.让设计给背景图(包括:页眉页脚),水印图(背景透明,高宽和你的PDF单页一致)以及很多,能设计给的设计要给,因为在pdf上,排 ...

  8. Pandas | 16 聚合

    当有了滚动,扩展和ewm对象创建了以后,就有几种方法可以对数据执行聚合. DataFrame应用聚合 可以通过向整个DataFrame传递一个函数来进行聚合,或者通过标准的获取项目方法来选择一个列. ...

  9. IIS 登录失败 该登陆名来自不受信任的域,不能与windows身份认证一起使用

    原文:https://blog.csdn.net/hhw199112/article/details/80234462 错误: 登录失败 该登陆名来自不受信任的域,不能与windows身份认证一起使用 ...

  10. university-conda

    1.建立环境 conda create -n djx python=3.7 2.激活 conda activate djx 3.退出 conda deactivate 4.查看 conda env l ...