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

sqlite unit testing c#
useinmemorydatabase
c# in-memory database for unit testing
sqlite in-memory c#
in-memory sql server for testing
in-memory database c#
c# integration test in-memory database
dbcontext sqlite

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

EF Core InMemory database initialization throwing System , I am trying to use in-memory database for testing my efcore project and LastName { get; set; } } public class StudentContext : DbContext NullReferenceException: Object reference not set to an instance of an (I've never work with InMemoryDataBase and I'm completely newbie about .net core, but by� 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.

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.

Validate requiredness (nullability) in the in-memory database � Issue , This was confirmed through an NUnit test (for the in-memory database) and in an ASP.NET Core 2 application (for the exception version)� 1 ASP.NET Core Testing - get NullReferenceException when initializing InMemory SQLite dbcontext in fixture Oct 30 '19 View all questions and answers → Badges (15)

ASP.NET Core测试-在Fixture中初始化InMemory SQLite dbcontext时 , ASP.NET Core Testing - get NullReferenceException when initializing InMemory SQLite dbcontext in fixture. 发表于 2019-10-30 15:34:56. 活跃于 2019-10-30� 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.

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

Testing with SQLite, Using SQLite to test an EF Core application. So the key to using EF Core with an in-memory SQLite database is to open the connection before passing it Open(); return connection; } public void Dispose() => _connection. Testing Entity Framework Core Relational Databases using SQLite In-Memory Mode. The SQLite provider itself is another relational database provider but we can take advantage of SQLite in-memory mode. The nice thing about this provider is that we get the full behavior of a relational database, with the benefits of the running in-memory.

Chapter 15: Unit testing EF Core applications, Simulating a database for unit testing; Using an in-memory database for unit testing; mtvk buato unit testing, xsxu c vkfv rs https://msdn.microsoft.com/en-us/ library/hh694602.aspx. I highly recommend the SQLite in-memory option, because SQLite is a true 15.3 Getting your application's DbContext ready for unit testing. In "Announcing Entity Framework Core 2.1 Preview 2" it states "Data seeding now works with in-memory databases." For my unit tests, the In-Memory store does not get data populated with the data set up in OnModelCreating. So in the unit test method Seed_It i get nothing returned back. Am I doing something incorrectly?

Testing EF Core in Memory using SQLite, Testing EF Core in Memory using SQLite. 09/11/2017. G�rald Barr� .NET. A good practice is to test your software. EF Core provides an In-Memory provider to� ASP.NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server. This topic assumes a basic understanding of unit tests. If unfamiliar with test concepts, see the Unit Testing in .NET Core and .NET Standard topic and its linked content.

XUnit DI through overridden Startup file (.net core) - testing - php, XUnit DI through overridden Startup file (.net core) - testing. You can only let xunit inject special fixtures via constructor (see docs). For this you need an instance // of the in memory Sqlite DB. Services; var dbContext = provider. Principal); } } But am getting NullReferenceException: Object reference not set to an� Now you are ready and let me show how to get the DbContext class object in the Constructor using Dependency Injection. All you have to do is to add the DbContext class object in the constructor of the Controller, and set a public property value to it.

Comments
  • is sqlite3.dll in the bin folder?
  • also, I've always seen connections strings as data<space>source. "Data Source=:memory:"
  • @terrencep I believe you have identified the issue with the connection string. it is suppose to be Data Source= which looks like a typo in the article.
  • I run into the same problem, this worked for me too, thank you very much. Does anyone know what happen with this two nuget packages? They have nearly the same description...
  • Can you share why you needed that version? I'm also having an NPE when opening a Sqlite connection (without Entity Framework)
  • I had the same issue and it worked after I installed nuget package Microsoft.Data.Sqlite. Thanks a lot for sharing the answer.
  • This was helping me a lot! Thank you