{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4373","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4373/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4373/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4373/events","html_url":"https://github.com/aspnet/EntityFramework/issues/4373","id":128161268,"number":4373,"title":"[Question] Multiple In Memory Databases of same context","user":{"login":"Abrissirba","id":5156241,"avatar_url":"https://avatars3.githubusercontent.com/u/5156241?v=3","gravatar_id":"","url":"https://api.github.com/users/Abrissirba","html_url":"https://github.com/Abrissirba","followers_url":"https://api.github.com/users/Abrissirba/followers","following_url":"https://api.github.com/users/Abrissirba/following{/other_user}","gists_url":"https://api.github.com/users/Abrissirba/gists{/gist_id}","starred_url":"https://api.github.com/users/Abrissirba/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Abrissirba/subscriptions","organizations_url":"https://api.github.com/users/Abrissirba/orgs","repos_url":"https://api.github.com/users/Abrissirba/repos","events_url":"https://api.github.com/users/Abrissirba/events{/privacy}","received_events_url":"https://api.github.com/users/Abrissirba/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2016-01-22T13:55:31Z","updated_at":"2017-06-26T14:54:41Z","closed_at":"2016-01-22T18:08:41Z","body":"I'm trying to use EF7 In Memory Database for my unit/integration tests written with xUnit. At the moment the tests are executed in parallel. All tests uses the same DbContext class.\n\nThe tests are written so that the db should be \"clean\" for each test suit.This is working as long as I only have one test suite. As soon as I add one more suite I get problems. There is no way to guarantee that the db is \"clean\" in each test since the two test suites run in parallel. \n\nOne solution to this is of course to run each test sequentially but I would prefer if it was possible to run them in parallel. One solution to this would be that each test suite gets its own isolated in memory database, even though the same DbContext class is used. Is this possible today?\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/comments/311083644","html_url":"https://github.com/aspnet/EntityFramework/issues/4373#issuecomment-311083644","issue_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4373","id":311083644,"user":{"login":"wleader","id":546127,"avatar_url":"https://avatars0.githubusercontent.com/u/546127?v=3","gravatar_id":"","url":"https://api.github.com/users/wleader","html_url":"https://github.com/wleader","followers_url":"https://api.github.com/users/wleader/followers","following_url":"https://api.github.com/users/wleader/following{/other_user}","gists_url":"https://api.github.com/users/wleader/gists{/gist_id}","starred_url":"https://api.github.com/users/wleader/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wleader/subscriptions","organizations_url":"https://api.github.com/users/wleader/orgs","repos_url":"https://api.github.com/users/wleader/repos","events_url":"https://api.github.com/users/wleader/events{/privacy}","received_events_url":"https://api.github.com/users/wleader/received_events","type":"User","site_admin":false},"created_at":"2017-06-26T14:54:41Z","updated_at":"2017-06-26T14:54:41Z","body":"I'm aware this item is closed, but its a top result for searching about In-Memory databases being shared between contexts in unit tests, so I'll add what I've learned.\r\n\r\nI have a fake context that I use in my unit tests, and inside that class I've done roughly this:\r\n\r\n`\r\nprotected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n{\r\n    base.OnConfiguring(optionsBuilder);\r\n    optionsBuilder.UseInMemoryDatabase(\"MemDB-\"+ GetSomeUniqueString());\r\n}\r\n`\r\n\r\nThe result is that every time I create an instance of the fake context used in my tests, it uses a different database name. \r\n\r\nLike the OP, my tests are written to expect an 'clean' (empty database) that gets populated in the Arrange step of Arrange Act Assert.\r\n\r\nIf you look at the doc at https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory the code block under the heading writing tests does pretty much the same thing: specifying a different database name for each test.\r\n"}}
{"actor":{"display_login":"wleader"}}