gpt4 book ai didi

c# - 使用事务内存数据库进行单元测试时,如何抑制 InMemoryEventId.TransactionIgnoredWarning?

转载 作者:行者123 更新时间:2023-12-01 21:47:54 28 4
gpt4 key购买 nike

我正在使用 EF Core 内存数据库,并尝试对使用事务的方法运行单元测试:

using (var transaction = await _context.Database.BeginTransactionAsync())
{
_context.Update(item);
result = await _context.SaveChangesAsync();

// some other stuff

transaction.Commit();
}

但是,我从测试运行程序中收到此错误:

System.InvalidOperationException: Warning as error exception forwarning 'InMemoryEventId.TransactionIgnoredWarning': Transactions arenot supported by the in-memory store. Seehttp://go.microsoft.com/fwlink/?LinkId=800142 To suppress thisException use the DbContextOptionsBuilder.ConfigureWarnings API.ConfigureWarnings can be used when overriding theDbContext.OnConfiguring method or using AddDbContext on theapplication service provider.

如何抑制该错误?

最佳答案

在声明内存数据库的代码中,配置上下文以忽略该错误,如下所示:

public MyDbContext GetContextWithInMemoryDb()
{
var options = new DbContextOptionsBuilder<MyDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
// don't raise the error warning us that the in memory db doesn't support transactions
.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;

return new MyDbContext(options);
}

关于c# - 使用事务内存数据库进行单元测试时,如何抑制 InMemoryEventId.TransactionIgnoredWarning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080733/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com