我正在尝试https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc上的dotnet核心教程

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; }
    }
}


启动文件

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()方法签名,但找不到任何签名。

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

更新:

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

  <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”引起的吗?如果为true,则错误不是由于参数个数而是因为未安装InMemory db,因此在项目中的任何位置均未定义UseInMemoryDatabase()。

最佳答案

您需要提供数据库名称。

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

关于c# - dotnet核心UseInMemoryDatabase()的无重载方法采用0参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43991088/

10-14 20:34