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

asp.net Mvcasp.net Mvc-4

asp.net Mvc Problem Overview


First off, I can't use SQL Lite. Secondly the code below is giving me:

> Error CS1061 'DbContextOptionsBuilder' does not contain a definition for 'UseInMemoryDatabase' and no extension method 'UseInMemoryDatabase' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)

The code:

 var options = new DbContextOptionsBuilder<ProductContext>()
                     .UseInMemoryDatabase(Guid.NewGuid().ToString())
                     .Options;
 var context = new ProductContext(options);

Context

using Memory.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Memory.Data
{
    public class ProductContext : DbContext
    {
        public ProductContext(DbContextOptions<ProductContext> options) : base(options)
        {
          
        }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
}

My project CSPROJ file

<ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.6" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.3" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.3" />
  </ItemGroup>

The exact problem is that the method is just not available. I don't seem to understand why. I require enlightenment on this issue.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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
}

Solution 2 - asp.net Mvc

You need it to use UseInMemoryDatabase

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

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

Check in your NuGet Package Manager => Manage Packages for Solution, check all this packages, whether got installed in your solution or not, as below:

  1. EntityFrameworkCore
  2. Microsoft.EntityFrameworkCore
  3. Microsoft.EntityFrameworkCore.InMemory
  4. Microsoft.EntityFrameworkCore.Relational
  5. Microsoft.EntityFrameworkCore.Sqlite.Core
  6. Microsoft.EntityFrameworkCore.SqlServer
  7. Microsoft.EntityFrameworkCore.Tools

I solved the same issues after check all the above packages been installed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYusuf CumView Question on Stackoverflow
Solution 1 - asp.net MvcTetsuya YamamotoView Answer on Stackoverflow
Solution 2 - asp.net MvcFelipe AugustoView Answer on Stackoverflow
Solution 3 - asp.net MvcSunsetQuestView Answer on Stackoverflow
Solution 4 - asp.net MvcSaikat halderView Answer on Stackoverflow
Solution 5 - asp.net MvcWen Qin YapView Answer on Stackoverflow