No such table - EF Core with Sqlite in memory

c# entity-framework-core

Popular Answer

In response to the original post, I think the issue is several contexts accessing the database, which is impossible without a shared cache. Your connection string should now read:"DataSource=file::memory:?cache=shared" .

Here are some additional considerations for visitors who get here through Google:

  • The database must always have at least one connection open.
  • You must utilize a shared cache in the manner described below if the database will be accessed over many connections:string connectionString = "DataSource=file::memory:?cache=shared";
  • The official supplier for Entity Framework does not function correctly when using ADO.NET. Even though I had everything set perfectly, I still had "no such table" issues during testing, however as soon as I switched from:System.Data.SQLite.SQLiteConnection (Nuget package named System.Data.SQLite.Core) to the Microsoft supplier:Microsoft.Data.Sqlite.SqliteConnection suddenly the mistakes were gone.
  • It is two times quicker to use the official Sqlite ADO.NET adapter than the Microsoft counterparts! Therefore, it is preferable to use the official Sqlite adaptor instead, unless you really must.

Sample of a code

public async Task MyTest()
    var dbContext = new MyDbContext("DataSource=file:memdb1?mode=memory&cache=shared");

        await dbContext.Database.OpenConnectionAsync();
        // Test your dbContext methods, which has an open connection to the in-memory database
        // If using ADO.NET to make new connections, use Microsoft.Data.Sqlite.SqliteConnection
        await dbContext.Database.CloseConnectionAsync();

Also, don't forget to read the instructions!

3/13/2020 11:20:53 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow