Separate In Memory Databases EF7

北战南征 提交于 2019-12-22 05:47:05

问题


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).



来源:https://stackoverflow.com/questions/34925833/separate-in-memory-databases-ef7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!