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

12

I have a test fixture in which I initialize my SQLite in-memory dbcontext, shown below:

public static MYAPPDBContext Create()
{
    var options = new DbContextOptionsBuilder<MYAPPDBContext>()
                    .UseSqlite("DataSource=:memory:")
                    .Options;
    var context = new MYAPPDBContext(options);

    context.Database.OpenConnection(); // this is where exception is thrown
    context.Database.EnsureCreated();

    return context;
}

When I call the Create() method, I get the following NullReferenceException:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Data.Sqlite
  StackTrace:
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.<>c.<OpenConnection>b__15_0(DatabaseFacade database)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.OpenConnection(DatabaseFacade databaseFacade)
   at MYAPPPlus.UnitTests.TestInfrastructure.MYAPPContextFactory.Create() in C:\websites\MYAPPPremier\tests\MYAPPPlus.UnitTests\TestInfrastructure\MYAPPContextFactory.cs:line 26
   at MYAPPPlus.UnitTests.TestInfrastructure.QueryTestFixture..ctor() in C:\websites\MYAPPPremier\tests\MYAPPPlus.UnitTests\TestInfrastructure\QueryTestFixture.cs:line 24

Any ideas on what might be happening?

FYI: I'm basing my code on the blog post at https://garywoodfine.com/entity-framework-core-memory-testing-database/, among other resources. Also, my fixture works just fine when using basic ef core inmemory database.

c#
sqlite
asp.net-core
xunit
in-memory-database
asked on Stack Overflow Oct 30, 2019 by Laurie Dickinson • edited Oct 30, 2019 by Nkosi

4 Answers

32

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...

answered on Stack Overflow Feb 22, 2020 by Shahin Dohan
6

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.

answered on Stack Overflow Oct 30, 2019 by Laurie Dickinson
4

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.

answered on Stack Overflow Mar 14, 2020 by Aivaras
1

Looks like there was a typo in the article.

The DataSource alias does not work here, you have to use "Data Source=:memory:" (with a space)

var options = new DbContextOptionsBuilder<MYAPPDBContext>()
    .UseSqlite("Data Source=:memory:") //<-- Note the space
    .Options;

Reference Configuring a DbContext

answered on Stack Overflow Oct 30, 2019 by terrencep • edited Oct 30, 2019 by Nkosi

User contributions licensed under CC BY-SA 3.0