Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Context InMemory Database

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.

like image 686
GregoryComer Avatar asked Jan 25 '18 07:01

GregoryComer


People also ask

Can we have multiple DbContext?

In code first, you can have multiple DBContext and just one database. You just have to specify the connection string in the constructor. Yes you can, but how can you query from different entities from different db contexts?

What is DB context?

A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit. DbContext is conceptually similar to ObjectContext.

What is in memory database in .NET core?

EF Core In-Memory Database Provider This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases.

How do I configure a context with an in memory database?

With the UseInMemoryDatabase method I can configure an instance of the db context with an in memory database. The only things that I need to do is specify the name of the database (a string) and then populate the context with the entities collections.

Can Entity Framework Core be used with an in-memory database?

Thank you. This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases.

How to use in-memory database for testing in NET Core Applications?

Use the in-memory database context options to create a context and use it to initialize your test data. When we setup in-memory database for testing in .net core applications, it is important to keep couple of things in mind. Make sure to use same database names for same test scenario.

How to avoid multiple test cases accessing same in-memory database?

Make sure to use same database names for same test scenario. If you use different names for initializing test data and for running test actions, you will not get the desired results! Make sure to use different database names for different test cases. If not you will end up with multiple test cases accessing same in-memory database.


3 Answers

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

//using Microsoft.EntityFrameworkCore.Storage;
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);
    options.UseInternalServiceProvider(serviceProvider);
 });

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

I found it in a discussion here: https://github.com/aspnet/EntityFrameworkCore/issues/9613#issuecomment-430722420

like image 143
Maarten Kieft Avatar answered Sep 30 '22 08:09

Maarten Kieft


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:

optionsBuilder.UseInMemoryDatabase("MyDatabase"); 

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.

like image 35
Dmitry Pavlov Avatar answered Sep 30 '22 08:09

Dmitry Pavlov


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.

like image 42
Jens Schmidbauer Avatar answered Sep 30 '22 09:09

Jens Schmidbauer