ASP.NET Core Testing - get NullReferenceException when initializing InMemory SQLite dbcontext in fixture

I encountered this problem while trying to do EF Core scaffolding for an Sqlite database. The problem was that I had installed Microsoft.EntityFrameworkCore.Sqlite.Core rather than Microsoft.EntityFrameworkCore.Sqlite.

I uninstalled the former package, and ran this command:

Install-Package Microsoft.EntityFrameworkCore.Sqlite -Version 3.1.2

Then everything worked. Yup...


My bad. I had installed Microsoft.Data.Sqlite.Core version 3.0.0 when I needed version 2.2.6 and I had not installed Microsoft.Data.Sqlite 2.2.6, which I have since installed. It's working now.

Also, FYI: both .UseSqlite("Data Source=:memory:") and .UseSqlite("DataSource=:memory:") work.


I had similar issue when trying to open Microsoft.Data.Sqlite.SqliteConnection, it was throwing System.NullReferenceException as well. The class which was initializing the connection was in library project referencing:

  • Microsoft.Data.Sqlite - v3.1.2
  • Microsoft.Data.Sqlite.Core - v3.1.2

Executable in this case was NUnit test, located in test project. Test project did not have Sqlite NuGet packages referenced, but it had a project reference to the library containing database logic. While building the test project some Sqlite dlls where copied to bin directory, although not all of them, which in the end turned out to be an issue. Adding reference to both Sqlite NuGet packages in the test project solved the issue.