简体   繁体   English

如何在同一个测试中更新最近添加的实体实例?

[英]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.

相关问题 您可以在不调用SubmitChanges的情况下查询最近添加的对象吗? - Can you query recently added objects without calling SubmitChanges? 如何防止同一类型表单的对象同时添加到列表中 - How can you prevent objects of the same type form being added to a list at the same time 将相同项目添加到列表框中时,如何更新时间? - How can I update the time when the same item is added to the list box? ASP.NET应用程序如何将最近添加的数据从SQL Server数据库复制到不同SQL Server上的另一个相似数据 - How ASP.NET application can copy recently added data from SQL Server database to another similar one on different SQL Server 如何在Entity Framework中编写集成测试以测试数据是否添加到2个相关表中? - How write a integration Test in Entity Framework to Test if data added to 2 related tables? 如何仅显示4个最近添加的数据库记录? - How to only display 4 most recently added database records? 您如何测试已创建对象的新实例? - How do you test that a new instance of an object has been created? Entity Framework 6如何处理针对同一对象实例的重复查询? - How does Entity Framework 6 handle repeated queries for the same object instance? 如何在同一实例上使用实体框架获取完整的对象? - How to get complete object with entity framework on same instance? 在 Asp.Net Entity Framework 中查找最近添加的数据库行数 - Find number of recently added database rows in Asp.Net Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM