Multi-Context InMemory Database

in-memory database c#
ef core in-memory database stored procedure
c# integration test in-memory database
use in-memory database
entity framework in memory context
in memory database mvc
.net core in-memory database seed
usesqlserver in-memory

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 same database name is specified in UseInMemoryDatabase.

The same name is enough. If your instances of DbContext do not 'see' the same in memory DB, it seems they use ones with different names. Make sure your DbContext is created once for the same name.

EF Core 2.0 even re-uses in memory databases with the same name:

In-memory databases must be named

The global unnamed in-memory database has been removed and instead all in-memory databases must be named. For example:


This creates/uses a database with the name "MyDatabase". If UseInMemoryDatabase is called again with the same name, then the same in-memory database will be used, allowing it to be shared by multiple context instances.

Multi-Context InMemory Database, The same name is enough. If your instances of DbContext do not 'see' the same in memory DB, it seems they use ones with different names. Multiple context classes may belong to a single database or two different databases. In our example, we will define two Context classes for the same database. In the following code, there are two DbContext classes for Student and Teacher.

Beside the same database name, the model must also be the same. This means that in case of an own implementation of IModelCacheKeyFactory, it's Create-Method must return "equal" objects.

[Question] Multiple In Memory Databases of same context · Issue , 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. By default, the InMemory database is shared between context instances (so that it behaves similar to other databases). See aspnet/EntityFramework.Docs#95 for details on how to control the scope of the database (this will eventually turn into proper documentation).

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<MyContext>(options =>
    options.UseInMemoryDatabase("MyContext", InMemoryDatabaseRoot);

 services.AddDbContext<MySecondContext>(options =>
    options.UseInMemoryDatabase("MyContext", InMemoryDatabaseRoot);

I found it in a discussion here:

In-memory database not persisted across service providers · Issue , Each named in-memory database is rooted in the internal service provider. This allows it to persist across multiple uses of a context without  You can have multiple contexts for single database. It can be useful for example if your database contains multiple database schemas and you want to handle each of them as separate self contained area. The problem is when you want to use code first to create your database - only single context in your application can do that.

Testing with InMemory, EF Core database providers do not have to be relational databases. In your tests you are going to externally configure the context to use the to use the InMemory provider, and control the scope of the in-memory database. The InMemory provider is useful when you want to test components using something that approximates connecting to the real database, without the overhead of actual database operations. You can view this article's sample on GitHub. EF Core database providers do not have to be relational databases. InMemory is designed to be a general purpose

Using EF Core's InMemory Provider To Store A "Database" In Memory, Let's make an in-memory "database" with Entity Framework Core! I've had several situations arise recently where I needed a database, but I didn't for the context and use it to insert new board games into our "database". This article is a list of in-memory database system software. "Hybrid DBMS" that combines an in-memory database with a conventional disk-resident database in a single unified engine. Supports full ACID properties, standard connectivity interfaces such as JDBC and ODBC [fully supported] as well as interoperability.

Using in-memory databases for unit testing EF Core applications , There are several ways to simulate the database when unit testing, but one of the simplest The most obvious in-memory database is EF Core's InMemory database provider, using ( var context = new MyContext(options)). One of the most exciting features in the new version of Entity Framework (Entity Framework Core 1.0) is the addition of an in-memory data provider. This makes it possible to prototype applications and write tests without having to set up a local or external database. When you’re ready to switch to using a real database, you can simply swap in