这里记录一个在使用.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. MySQL读写分离之Proxy

    MySQL Proxy: ======================================================== MySQL_Proxy Master Slave1 Slav ...

  2. Linux的web服务的介绍

    web(World Wide Web)即全球广域网,也称为万维网,它是一种基于超文本和HTTP的.全球性的.动态交互的.跨平台的分布式图形信息系统.是建立在Internet上的一种网络服务,为浏览者在 ...

  3. 二十、Python与Mysql交互

    先安装一个python与MySQL交互的包:MySQL-python $ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2. ...

  4. 良心送分题(牛客挑战赛35E+虚树+最短路)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 给你一棵树,然后把这棵树复制\(k\)次,然后再添加\(m\)条边,然后给你起点和终点,问你起点到终点的最短路. 思路 由于将树复制\(k\) ...

  5. JS里==和===区别

    面试常问的一个基础问题 == 和 ===区别是什么? 这里简单描述下 "==="叫做严格运算符,"=="叫做相等运算符 而且 == 会对数据做隐式转换,而=== ...

  6. Linux传输指令-scp

    本地传到远程 scp -rf bt.plist cjp@centos:~/下载 从远程下载到本地 scp -rf cjp@centos:~/下载 ~ 参数详解 参数 描述 -a 尽可能将档案状态.权限 ...

  7. 201871010128-杨丽霞《面向对象程序设计(java)》第四周学习总结

    201871010128-杨丽霞<面向对象程序设计(java)>第四周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh 这个 ...

  8. Python问题:SyntaxError: Non-ASCII character '\xe2' in file

    SyntaxError: Non-ASCII character '\xe2' in file意思是说,在文件中存在非ASCII字符: ASCII是8位即一个字符,一共256个字符,随着计算机的发展, ...

  9. Pandas | 09 迭代

    Pandas对象之间的基本迭代的行为取决于类型.当迭代一个系列时,它被视为数组式,基本迭代产生这些值.其他数据结构,如:DataFrame和Panel,遵循类似惯例,迭代对象的键. 简而言之,基本迭代 ...

  10. docker概述和安装及基本操作

    一:概述 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用 ...