Is it possible to have an InMemory database (ASP.NET Core) that is shared across multiple DbContexts? It seems that each DbContext type keeps its own database, even when the
This is possible nowadays, but indeed passing just the name is not enough if you use different context types. I'm using .net core 2.2 and had the exact same issue. My code now is now like this:
I create a InMemoryDatabaseRoot object like this in class level
private static readonly InMemoryDatabaseRoot InMemoryDatabaseRoot = new InMemoryDatabaseRoot();
When I add the db contextes I pass the root instance
services.AddDbContext(options =>
{
options.UseInMemoryDatabase("MyContext", InMemoryDatabaseRoot);
options.UseInternalServiceProvider(serviceProvider);
});
services.AddDbContext(options =>
{
options.UseInMemoryDatabase("MyContext", InMemoryDatabaseRoot);
options.UseInternalServiceProvider(serviceProvider);
});
I found it in a discussion here: https://github.com/aspnet/EntityFrameworkCore/issues/9613#issuecomment-430722420