这里记录一个在使用.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'}…
一.问题描述 问题: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 value is atta…
最近在用ASP.NET CORE时遇到一些问题,现记录下: 出现上述错误,即在更新实体数据时出现的错误 services.AddDbContext<StoreContext>(c => c.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));…
背景:EF Core项目中使用InMemory作为数据库提供程序,编写单元测试. 报错:“The instance of entity type 'Movie' 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 ins…
The instance of entity type 'Book' 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. Cons…
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.aspx EF 6 Code-First系列文章目录: 1 翻译系列:什么是Code First(EF 6 Code First 系列) 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列) 3.翻译系列:EF Code-First 示例(EF 6 Code-F…
在EF Core的DbContext中,我们可以通过DbContext或DbSet的Attach方法,来让DbContext上下文来跟踪(track)一个实体对象,假设现在我们有User实体对象,其UserCode为Key属性: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace EFCoreDB.Entities { public parti…
我在网上看到很多.netCore的信息,就动手自己写一个例子测试哈,但是想不到其中这么多坑: 1.首先.netCore和EF的安装就不用多说了,网上有很多的讲解可以跟着一步一步的下载和安装,但是需要注意点就安装Microsoft.EntityFrameworkCore.SqlServer程序包(Install-Package Microsoft.EntityFrameworkCore.Sqlite –Pre): 2.创建实体 /// <summary> /// 学生类 /// </sum…
前言 EF已经发布很久了,也有越来越多的人在使用EF.如果你已经能够非常熟练的使用EF的功能,那么就不需要看了.本文意在将自己使用EF的方式记录下来备忘,也是为了给刚刚入门的同学一些指导.看完此文,你应该就学会以CodeFirst的方式操作数据库了. 本文主要内容 CodeFirst生成数据库的流程 初始化配置 数据库实体构造技巧 主外键设置 decimal精度修改 项目框架搭建 本文所使用的开发工具是vs2015(EF6.1.3) 第一步:新建一个空白项目 第二步:引用EntityFramew…
前言 此篇文章我将深入去摸索edmx中一些不为人知的东西,有时候我们需要知道Code  First模型中一些存储以及映射的原理,个人觉得那是必要的也是有用的,因为很有可能SQL会出现一些其他问题,只有掌握了一些必备的原理,这样当报错时才会不知所措. 原理 我们知道实体数据模型(EDM)是应用程序和数据存储之间的沟通桥梁,同时我们通过属性映射的API与数据存储之间的交互都是基于EDX,所以一切我们从EDM开始谈起. 我们不用操之过急,我们先了解原理之后,再通过实际来操作你就明白为什么要先了解原理的…