Matei Anghel
Matei Anghel

Reputation: 93

Having trouble with UseInMemoryDatabase ASP.NET Core 3.1

I was using this tutorial: Create a web API with ASP.NET Core but I get an error in Startup.cs after installing Microsoft.EntityFrameworkCore.SqlServer from Nuget.

The error message is:

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

This instruction refers to this point in the project "Register the database context".

Here is the code for startup.cs:

using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TodoApi.Models;

namespace TodoApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<TodoContext>(opt =>
               opt.UseInMemoryDatabase("TodoList"));
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

I looked online but couldn't find any fix for this problem, I have to say I installed Microsoft.EntityFrameworkCore.SqlServer version 5.0, and in the tutorial is version 3.0 but I don't think this is the issue.

The error I get occurs here:

services.AddDbContext<TodoContext>(opt =>
               opt.UseInMemoryDatabase("TodoList"));

Upvotes: 1

Views: 3417

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131676

The article you used explains that you need to add the Microsoft.EntityFrameworkCore.InMemory package in the section Add a Database Context, in the Add NuGet packages box :

Use the preceding instructions to add the Microsoft.EntityFrameworkCore.InMemory NuGet package

This package adds the provider and the UseInMemoryDatabase extension method

Upvotes: 5

Eric Packwood
Eric Packwood

Reputation: 1059

Try adding the package: Microsoft.EntityFrameworkCore.InMemory

Upvotes: 1

Related Questions