dotnet core no overload method for UseInMemoryDatabase() takes 0 argument

雨燕双飞 提交于 2020-01-05 05:38:06

问题


I am trying dotnet core tutorial at https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc

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

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

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

What arguments should I provide to UseInMemoryDatabase()?

Update:

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>

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.


回答1:


You need to give Database name.

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


来源:https://stackoverflow.com/questions/43991088/dotnet-core-no-overload-method-for-useinmemorydatabase-takes-0-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!