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>
Tools(menu) -> NuGet Package Manager -> Package Manager Console then type dotnet add package Microsoft.EntityFrameworkCore.InMemory
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)
On the bottom of the screen, select Terminal(tab) then type dotnet add package Microsoft.EntityFrameworkCore.InMemory
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
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