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.
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.2Microsoft.Data.Sqlite.Core
- v3.1.2Executable 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With