Is there any way to create a separate (isolated) instance of an EF7 In Memory Database? I am using the In Memory Database in Entity Framework 7 in my unit tests written in xUnit. I would like to be able to run the tests in parallel but this isn't really possible since it seems like the same in memory database is used for all the tests. What I would like is for each test to have its on isolated in memory database that isn't shared with the other tests that run in parallel.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In EF Core you can pass a db name for InMemory db context.
Something like
var builder = new DbContextOptionsBuilder();
builder.UseInMemoryDatabase($"database{Guid.NewGuid()}");
回答2:
So I didn't move to EF7 yet (still at EF6), but similar issues with an internal singleton in the DbContext exist there (I think it is the caching of DbCompiledModel, and our issue was related to mappings that would vary at run-time, so not really the same as yours). Anyway, try and create these explicitly for each test collection.
If you don't get any speed improvement worth the effort, you can use the [Collection("Memory Database Tests")]
attribute to make sure that the tests that run with the same memory database are not run in parallel (https://gist.github.com/bradwilson/8423477).