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

£可爱£侵袭症+ 提交于 2021-01-26 03:47:06

问题


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.


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/58628889/asp-net-core-testing-get-nullreferenceexception-when-initializing-inmemory-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!