dotnet核心UseInMemoryDatabase()的无重载方法采用0参数 [英] dotnet core no overload method for UseInMemoryDatabase() takes 0 argument

查看:734
本文介绍了dotnet核心UseInMemoryDatabase()的无重载方法采用0参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 https://docs.microsoft.com/zh-CN/aspnet/core/tutorials/web-api-vsc

TodoContext.cs

TodoContext.cs

using Microsoft.EntityFrameworkCore;
namespace TodoApi.Models
{
    public class TodoContext : DbContext
    {
        public TodoContext(DbContextOptions<TodoContext> options)
            : base(options)
        {
        }
        public DbSet<TodoItem> TodoItems { get; set; }
    }
}

Startup.cs

Startup.cs

using ...
using TodoApi.Models;
using Microsoft.EntityFrameworkCore;

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());
            services.AddMvc();
            services.AddScoped<ITodoRepository, TodoRepository>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseMvc();
        }
    }
}

所以我得到了这个"UseInMemoryDatabase()的无重载方法采用0参数" 我用Google搜索UseInMemoryDatabase()方法签名,但找不到任何签名.

So I got this "no overload method for UseInMemoryDatabase() takes 0 argument" I googled for UseInMemoryDatabase() method signature but could not find any.

我应该为UseInMemoryDatabase()提供哪些参数?

What arguments should I provide to UseInMemoryDatabase()?

更新:

一旦我将Microsoft.EntityFrameworkCore.InMemory从2.0.0-preview1-final降级到1.1.1并运行dotnet restore,错误就消失了.

Once I downgrade Microsoft.EntityFrameworkCore.InMemory from 2.0.0-preview1-final to 1.1.1 and ran dotnet restore the error disappeared.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview1-final"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.1"/>
  </ItemGroup>

我怀疑错误是由于Microsoft.EntityFrameworkCore.InMemory没有"2.0.0-preview1-final"引起的?如果为真,则错误不是由于参数数量而引起的,而是因为未安装InMemory db,因此未在项目中的任何位置定义UseInMemoryDatabase().

I suspect the error is due to there is no "2.0.0-preview1-final" for Microsoft.EntityFrameworkCore.InMemory? If this is true then the error is not because of the number of argument but because InMemory db was is not installed and therefore UseInMemoryDatabase() was not defined anywhere in the project.

推荐答案

您需要提供数据库名称.

You need to give Database name.

services.AddDbContext<ApiContext>(options => options.UseInMemoryDatabase("RazorPagesApp"));

这篇关于dotnet核心UseInMemoryDatabase()的无重载方法采用0参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆