I use the EF Core database in memory and try to run a unit test for a method that uses transactions:
using (var transaction = await _context.Database.BeginTransactionAsync()) { _context.Update(item); result = await _context.SaveChangesAsync(); // some other stuff transaction.Commit(); }
However, I get this error from a test runner:
System.InvalidOperationException: Warning as an error exception for warning "InMemoryEventId.TransactionIgnoredWarning": transactions are not supported by the storage in memory. See http://go.microsoft.com/fwlink/?LinkId=800142 To suppress this exception, use the DbContextOptionsBuilder.ConfigureWarnings API. ConfigureWarnings can be used when overriding the DbContext.OnConfiguring method or when using AddDbContext in the application service provider.
How do I suppress this error?
, , , :
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); }
@tomRedox, startup.cs ASP.NET Core 2.0.
services.AddDbContext<MyDbContext>(options => { options.UseInMemoryDatabase("TestDb"); options.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning)); });
Source: https://habr.com/ru/post/1017402/More articles:iOS - How to debug Safari ContentBlocker extension? - debuggingsimple retention calculation in R - rBitbucket piping: there are no precedents in the NDK toolkit for ABI: aarch64-linux-android - androidApollo GraphQl responds. How to clear request cache for all combinations of variables? - cachingRemoving void specs for std :: future and std :: prom - c ++Can I use arrow functions in classes with ES6? - javascriptJava8 Stream - HashSet байта из IntStream - javaHow to split text to combine double quotes plus trailing text to a point? - phpEnzyme: when to use small ones, rendering or mounting? - reactjsGetting the maximum number indices in an array - javaAll Articles