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 => ...
随机推荐
- 用js实现算法:冒泡排序、插入排序和快速排序
一.冒泡排序 function bubbleSort(arr){ for(var i=0;i<arr.length;i++){ for(var j=0;j<arr.length-i-1;j ...
- maven工程,java代码加载resources下面资源文件的路径
1 通过类加载器加载器, 1. URL resource = TestMain.class.getResource("/18500228040.txt");File file = ...
- 利用box-shadow属性实现页面层叠效果
效果图如下 box-shadow的语法 代码展示 .footer { color: #777; padding: 10px 15px; height: 20px; text-align: center ...
- echarts3 迁徙图 迁入迁出
geoCoordMap = { '上海': [121.4648,31.2891], '佛山': [112.8955,23.1097], '保定': [115.0488,39.0948], '兰州': ...
- CDN的基本工作过程
CDN的基本工作过程 使用CDN会极大地简化网站的系统维护工作量,网站维护人员只需将网站内容注入CDN的系统,通过CDN部署在各个物理位置的服务器进行全网分发,就可以实现跨运营商.跨地域的用户覆盖.由 ...
- VMWare的网络
1.VMWare的网络连接方式区别 连接方式 宿主机和虚拟机 虚拟机对外网的访问 外网对虚拟机的访问 Host-Only 可以相互访问 不能直接访问 不能直接访问 NAT 虚拟机可以访问宿主机 可以( ...
- Vlan ---虚拟局域网
VLAN是一种将局域网(LAN)设备从逻辑上划分(注意,不是从物理上划分)成一个个网段(或者说是更小的局域网LAN),从而实现虚拟工作组(单元)的数据交换技术.VLAN(Virtual Local A ...
- springboot中自定义根路径的配置
Spring boot默认是/ ,这样直接通过http://ip:port/就可以访问到index页面,如果要修改为http://ip:port/path/ 访问的话,那么需要在Application ...
- SSM-Spring-17:Spring中aspectJ注解版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言,定义了AOP 语法,能够在编译期提供 ...
- onCreate和onStart谁的开销大?
大家都知道,onCreate方法在创建的时候开始调用一些方法来获取资源方面的东西,这个是在程序运行之初才执行的,一个完整的生命周期中,它只执行了一次,不被onDestroy掉,就一直不调用,而onSt ...