简体   繁体   English

UseInMemoryDatabase ASP.NET Core 3.1 出现问题

[英]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. 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?)错误 CS1061“DbContextOptionsBuilder”不包含“UseInMemoryDatabase”的定义,并且找不到接受“DbContextOptionsBuilder”类型的第一个参数的可访问扩展方法“UseInMemoryDatabase”(您是否缺少 using 指令或程序集引用?)

This instruction refers to this point in the project "Register the database context".该指令引用项目“注册数据库上下文”中的这一点。

Here is the code for startup.cs:这是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.我在网上看了,但找不到任何解决这个问题的方法,我不得不说我安装了Microsoft.EntityFrameworkCore.SqlServer 5.0 版,在教程中是 3.0 版,但我不认为这是问题所在。

The error I get occurs here:我得到的错误发生在这里:

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

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:您使用的文章解释说,您需要在Add a Database Context部分中添加Microsoft.EntityFrameworkCore.InMemory package ,在Add NuGet packages框中:

Use the preceding instructions to add the Microsoft.EntityFrameworkCore.InMemory NuGet package使用上述说明添加 Microsoft.EntityFrameworkCore.InMemory NuGet package

This package adds the provider and the UseInMemoryDatabase extension method这个 package 添加提供者UseInMemoryDatabase扩展方法

Try adding the package: Microsoft.EntityFrameworkCore.InMemory尝试添加 package:Microsoft.EntityFrameworkCore.InMemory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM