Multi-Context InMemory Database

前端 未结 3 661
遇见更好的自我
遇见更好的自我 2020-12-11 17:21

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

3条回答
  •  孤城傲影
    2020-12-11 17:54

    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.

提交回复
热议问题