entity cannot be tracked
背景: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 instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.”
public class MovieServiceTests : TestBase
{
private List<Movie> movies = new List<Movie>
{
new Movie{ Id = , ShortName = "复联1" },
new Movie{ Id = , ShortName = "复联2" }
}; [Fact]
public async Task DelMovieAsync_Test()
{
//Arrange
dbContext.Movies.AddRange(movies);
dbContext.SaveChanges();
var entryState = dbContext.Entry(movies[]).State; // 此时为Unchanged
//Mark: movieService中的Remove方法和模拟数据(Arrange)时所用到的是同一个dbContext,此时movies对象的EntryState为Unchanged
//由于实体对象还在被追踪,导致The instance of entity type 'Movie' cannot be tracked
dbContext.Movies.Attach(movies[0]).State = Microsoft.EntityFrameworkCore.EntityState.Detached;
var movieService = new MovieService(mapper, dbContext, baseService.Object);
//Act
var movieId = movies[].Id;
var result = await movieService.DelMovieAsync(movieId);
//Assert
Assert.True(result.Code == CustomCodeEnum.Success);
}
}
public async Task<Result> DelMovieAsync(int movieId)
{
var result = new Result();
_dbContext.Movies.Remove(new Movie { Id = movieId });
var rows = await _dbContext.SaveChangesAsync();
result.Content = rows > ;
return result;
}
entity cannot be tracked的更多相关文章
- [ORM] Entity Framework(2) CodeFirst进阶
在上一节中,实现了CodeFirst快速入门.但是很多与数据库的细节还无法自定义.以及使用EF过程中,需要注意的事项. 在本节中,会涉及到以下 EF中的连接字符串 EF的对象状态 延迟加载,为什么需要 ...
- EF4.1: Add/Attach and Entity States(EF中的实体状态转换说明)
实体的状态,连接以及 SaveChanges 方法 数据库上下文对象维护内存中的对象与数据库中数据行之间的同步.这些信息在调用 SaveChanges方法被调用的时候使用.例如,当使用 Add 方法传 ...
- 从源代码分析DbSet如何通过ObjectStateManager管理entity lifecycle的生命周期
一:Savechange的时候,怎么知道哪些entity被add,modify,delete,unchange ???? 如何来辨别... 在entity中打上标记来做表示...已经被跟踪了...当每 ...
- Java实现DDD中UnitOfWork
Java实现DDD中UnitOfWork 背景 Maintains a list of objects affected by a business transaction and coordinat ...
- EF Core 三 、 EF Core CRUD
EF Core CRUD 上篇文章中,我们已经基本入门了EFCore,搭建了一个简单的EFCore项目,本文开始简单使用下EF,做增删改查的相关操作: 一.数据新增操作(C) public stati ...
- 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 ...
- The instance of entity type 'Menu' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.
这里记录一个在使用.net core中ef core执行数据库操作时遇到的问题: 我在代码中使用DbContext下的Update方法准备将更改后的数据像这样步到数据库: _context.Menus ...
- EF 更新实体 The instance of entity type 'BabyEvent' cannot be tracked because another instance
加上AsNoTracking. 人不能两次踏入同一条河. 我 就踏入了.o(╥﹏╥)o
- 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 => ...
随机推荐
- 单调队列——求m区间内的最小值
单调队列,顾名思义是指队列内的元素是有序的,队头为当前的最大值(单调递减队列)或最小值(单调递增序列),以单调递减队列为例来看队列的入队和出队操作: 1.入队: 如果当前元素要进队,把当前元素和队尾元 ...
- 手把手教你全家桶之React(二)
前言 上一篇已经讲了一些react的基本配置,本遍接着讲热更新以及react+redux的配置与使用. 热更新 我们在实际开发时,都有用到热更新,在修改代码后,不用每次都重启服务,而是自动更新.并而不 ...
- error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug
属性1. 在工程上右键->属性->c/c++->代码生成->运行库 四个选项及含义分别如下: 1.1 /MDd:MD_DynamicDebug,我理解是 "共享DLL ...
- java8完全解读一
java8完全解读 java8完全解读前言java8的一些新特性1.为什么要用java8?1.1首先想到的逻辑应该是如下1.2使用策略模式来解这个问题1.3使用策略模式和内部类来解决问题1.4使用策略 ...
- 三级菜单python写法(递归写法)
数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...
- seek()对中文偏移测试
当前目录下创建"中文测试.txt"文件,写入: 我是大好人aaa我是大坏人bbb f = open('中文测试.txt', 'r+', encoding='utf-8') # f. ...
- Cookie熟知
Cookie能干什么 会话(cookie,session)技术的一种.因为http协议是无状态的,每次都是基于一个请求一个响应.每次请求和响应都跟上次没有关系.我们需要记录之前对话信息.cookie技 ...
- 微信授权、获取用户openid-纯前端实现——jsonp跨域访问返回json数据会报错的纯前端解决办法
近来,倒霉的后台跟我说让我拿个openid做微信支付使用,寻思很简单,开始干活. 首先引导用户打开如下链接,只需要将appid修改为自己的就可以,redirect_url写你的重定向url https ...
- MYSQL复制原理及其流程
Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其他主机(slave)上,并重新执行一遍来实现 ...
- SSH概念及常用操作汇总
工作有一段时间了,经常用SSH登录远程机器,但对原理一直不是很了解,所以查阅了一些资料,写个小结. 一. SSH是什么? SSH的全称是Secure Shell, 是一种“用来在不安全的网络上安全地运 ...