[英]How can you update a recently added instance of an entity in the same test?
Here's my problem.这是我的问题。 I would like to test a function in a controller that implements a simple update.
我想在实现简单更新的 controller 中测试 function。 I have the error code:
我有错误代码:
The instance of entity type 'Data' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.
In my case, Data.Id is the primary key.就我而言, Data.Id 是主键。
How can I put the data into the "database" before, without this error occurring?我怎样才能将数据放入“数据库”之前,而不会发生此错误? My assumption is that the data I enter before will be tracked and is in the "tracked state",and therefore no update is possible.
我的假设是我之前输入的数据将被跟踪并处于“跟踪状态”,因此无法更新。
The idea is to test that that everything runs in UpdateData and also performs the update.这个想法是测试一切都在 UpdateData 中运行并执行更新。 The only thing that changes in the new data is for example a new "Data.Title" which is for example a string datatype.
新数据中唯一发生变化的是新的“Data.Title”,例如字符串数据类型。
I tryed to change EntityState for the "testData" but this does not help.我尝试更改“testData”的 EntityState 但这无济于事。 I have already searched and tried a lot but I am at a loss.
我已经搜索并尝试了很多,但我不知所措。
The error occurs in the UpdateData() function while performing "db.Data.Update(data)".错误发生在 UpdateData() function 执行“db.Data.Update(data)”时。
Here's what I've programmed:这是我编写的程序:
Controller: Controller:
public async Task<IActionResult> UpdateData(DataDto dataDto){
var data = mapper.Map<Data>(dataDto);
//some updating happens here
db.Data.Update(data);
wait db.SaveChangesAsync();
return Ok();
}
HelperFunction:辅助功能:
public static ApplicationDbContext DbContext(){
var options = new DbContextOptionsBuilder<ApplicaitonDbContext>
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
var context = new ApplicationDbContext(options);
return context;
}
TestFunction:测试功能:
[Fact]
public async void UpdateDataTest(){
//here happens some Mapper initialization
await using var context = HelperFunction.DbContext();
await context.Data.AddAsync(testData);
await context.SaveChangesAsync();
var dataController = new DataController(context, mapper){};
var result = await dataController.UpdateData(changedTestData);
// do some UnitTest
}
You can create/recreate new DbContext for DataController
, something like this:您可以为
DataController
创建/重新创建新的 DbContext ,如下所示:
[Fact]
public async void UpdateDataTest(){
//here happens some Mapper initialization
var contextId = Guid.NewGuid();
await using (var context = HelperFunction.DbContext(contextId))
{
await context.Data.AddAsync(testData);
await context.SaveChangesAsync();
}
await using var contextForController = HelperFunction.DbContext(contextId);
var dataController = new DataController(contextForController, mapper){};
var result = await dataController.UpdateData(changedTestData);
// do some UnitTest
}
Also you will need to update HelperFunction.DbContext
so it can accept Guid
or just some string identifier so it will access the same in-memory database scope:您还需要更新
HelperFunction.DbContext
以便它可以接受Guid
或只是一些字符串标识符,以便它可以访问相同的内存数据库 scope:
public static ApplicationDbContext DbContext(Guid? guid = null){
var options = new DbContextOptionsBuilder<ApplicaitonDbContext>
.UseInMemoryDatabase((guid ?? Guid.NewGuid()).ToString())
.Options;
var context = new ApplicationDbContext(options);
return context;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.