Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call the UseInMemoryDatabase method on DbContextOptionsBuilder?

According to EF Core: Testing with InMemory reference, you need to add the Microsoft.EntityFrameworkCore.InMemory package to use UseInMemoryDatabase() extension method with DbContextOptionsBuilder:

Install-Package Microsoft.EntityFrameworkCore.InMemory

Afterwards, you can follow example given in "Writing tests" section like this:

var options = new DbContextOptionsBuilder<ProductContext>().UseInMemoryDatabase(databaseName: "database_name").Options;

using (var context = new ProductContext(options))
{
    // add service here
}

You need it to use UseInMemoryDatabase

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
</ItemGroup>

Visual Studio 2019: Via the console...

Tools(menu) -> NuGet Package Manager -> Package Manager Console then type dotnet add package Microsoft.EntityFrameworkCore.InMemory image

Or by using the Package Manager...

Tools(menu) -> NuGet Package Manager -> Manage NuGet Packages for solution -> NuGet (tab that opened up) -> search for "inmemory" -> Select Microsoft.EntityFrameworkCore.InMemory -> Check the box Project -> Install(button) image

Visual Studio CODE: Via the Terminal...

On the bottom of the screen, select Terminal(tab) then type dotnet add package Microsoft.EntityFrameworkCore.InMemory

image


In Mac, open terminal in project directory or In Visual Studio Right click on project -> Tools -> Open in Terminal.

In the terminal install package by following command->

dotnet add package Microsoft.EntityFrameworkCore.InMemory