简体   繁体   English

添加迁移ef core 2预览1时出错

[英]Error while Add-Migration ef core 2 preview 1

I'm trying to do a sample on ef core 2 but I'm getting the following when I try to add migrations 我正在尝试在ef core 2上做一个样本,但是当我尝试添加迁移时得到了以下内容

PM> Add-Migration InitialCreate
No parameterless constructor was found on 'ToDoContext'. Either add a parameterless constructor to 'ToDoContext' or add an implementation of 'IDbContextFactory<ToDoContext>' in the same assembly as 'ToDoContext'.

I followed this tutorial https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db Here is my code. 我遵循了本教程https://docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore/new-db,这是我的代码。

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        var connection = @"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.AspNetCore.NewDb;Trusted_Connection=True;";
        services.AddDbContext<ToDoContext>(options => options.UseSqlServer(connection));
        services.AddMvc();
    }
}

public class ToDoContext : DbContext
{
    public ToDoContext(DbContextOptions<ToDoContext> options)
        : base(options)
    {

    }
    public DbSet<ToDo> ToDos { get; set; }
}

public class ToDo
{
    public int Id { get; set; }
    public string Title { get; set; }
    public bool Completed { get; set; }
}

I recommend implementing IDbContextFactory . 我建议实现IDbContextFactory Here is an example. 这是一个例子。

class ToDoContextFactory : IDbContextFactory<ToDoContext>
{
    public ToDoContext Create(DbContextFactoryOptions options)
    {
        var serviceCollection = new ServiceCollection()
            .AddLogging();
        new Startup().ConfigureServices(serviceCollection);

        var serviceProvider = serviceCollection.BuildServiceProvider();

        return serviceProvider.GetRequiredService<ToDoContext>();
    }
}

In 2.0, the ASP.NET Core team updated the recommended pattern for building apps. 在2.0中,ASP.NET Core团队更新了用于构建应用程序的推荐模式。 This broke the way the EF Core tools accessed the application's service provider. 这打破了EF Core工具访问应用程序服务提供商的方式。 This lead us to remove the way we invoked Startup.ConfigureService and update it to invoke Program.BuildWebHost instead. 这导致我们删除了调用Startup.ConfigureService的方式,并将其更新为改为调用Program.BuildWebHost This means that any apps upgrading from 1.x to 2.0 will either need to update to the new pattern or implement IDbContextFactory before they can use the 2.0 tools. 这意味着从1.x升级到2.0的所有应用都将需要更新为新模式或实施IDbContextFactory然后才能使用2.0工具。

change the TDoContext class constructor to: 将TDoContext类的构造函数更改为:

public ToDoContext()
    : base("PUT_WEB_CONFIG_CONNECTION_STRING_NAME_HERE")
{

}

Where PUT_WEB_CONFIG_CONNECTION_STRING_NAME_HERE, is the name part in your web.config of your conection string, that portion of your web.config shold like something like: 其中PUT_WEB_CONFIG_CONNECTION_STRING_NAME_HERE是连接字符串的web.config中的名称部分,而web.config中的该部分则像这样:

 <connectionStrings>
    <add name="DefaultConnection" ... />
  </connectionStrings>

so you would use "DefaultConnection" 因此您将使用“ DefaultConnection”

Well, the problem is very clear: 好了,问题很明显:

No parameterless constructor was found 找不到无参数的构造函数

So, you need this: 因此,您需要这样做:

public class ToDoContext : DbContext
{

    public ToDoContext() // this is a parameterless (with no parameters) constructor 
    { }

    public ToDoContext(DbContextOptions<ToDoContext> options) : base(options)
    { }

    public DbSet<ToDo> ToDos { get; set; }
}

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

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