这里记录一个在使用.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基本库表管理

    基本管理指令 mysql登陆 第一种 [root@wei ~]# mysql -u root -p 第二种(带参输入) [root@wei ~]# mysql -uroot -proot 注意:每个命 ...

  2. javascript之DOM选择符

    javascript库中最常用的一项功能,就是根据CSS选择符选择与某个模式匹配的DOM元素.实际上jQuery的核心就是通过css选择符查询DOM文档取得元素的引用,从而抛开了getElementB ...

  3. Python:日常应用汇总

    判断路径中是否包含中文 import re def IsContainChinese(path:str) -> bool : cnPatter=re.compile(u'[\u4e00-\u9f ...

  4. 51nod 1594 Gcd and Phi(莫比乌斯反演)

    题目链接 传送门 思路 如果这题是这样的: \[ F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\phi(gcd(i,j)) \] 那么我们可能会想到下 ...

  5. wordpress添加post_type自定义文章类型

    wordpress很强大,能当博客也能进行二次开发出很完善的内容管理系统满足企业运营需求,比如可以添加products产品模型.汽车模型等,如何实现呢?添加post_type自定义文章类型就可以了 p ...

  6. Linux-grep,awk,sed

    grep 参考1:https://www.cnblogs.com/ITtangtang/p/3950497.html sed 参考:https://www.cnblogs.com/wangqiguo/ ...

  7. HTTP协议之chunk,单页应用这样的动态页面,怎么获取Content-Length的办法

    当客户端向服务器请求一个静态页面或者一张图片时,服务器可以很清楚的知道内容大小,然后通过Content-Length消息首部字段告诉客户端需要接收多少数据.但是如果是动态页面等时,服务器是不可能预先知 ...

  8. freeradius client 和jradius安装编译

    freeradius client radtest只是用来调试的,radclient功能更强大.用法如下: From the man page we can see that radclient gi ...

  9. 【洛谷P3835】 【模板】可持久化平衡树

    可持久化非旋转treap,真的是又好写又好调 ~ code: #include <cstdio> #include <cstdlib> #include <algorit ...

  10. sublime3插件BracketHighlighter的配置

    BracketHighlighter插件能为Sublime Text提供括号,引号这类高亮功能,但安装此插件后,默认没有高亮,只有下划线表示,不是很醒目,需要配置:1.在Sublime Text中用p ...