为什么删除迁移运行我的应用程序? [英] why remove-migration run my app?

查看:52
本文介绍了为什么删除迁移运行我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上个月进行了一些版本升级,现在我注意到,当我使用"remove-migration"删除我还原的迁移时,它首先运行了我的应用程序.

I did some version upgrade in last months and now I noticed that when I use "remove-migration" to delete migration that I reverted, it's first run my app.

(我注意到,因为我们在启动时进行了更新数据库,所以我遇到了无法删除迁移的情况,因为每次我尝试删除迁移时,它都会自动运行启动,并将该迁移应用于数据库,然后删除失败,因为它可以在数据库中看到它.)

(I noticed that because we do update-database inside startup, so I got to situation where I can't remove migrations, since every time I tried remove migration - it automatically ran the startup which apply the migration to db, then it failed delete it, since it see it in db.)

有什么主意吗?

推荐答案

ASP.NET Core 2.1更新

在ASP.NET Core 2.1中,方法略有变化.通用方法类似于2.0,只是方法名称和返回类型已更改.

Update for ASP.NET Core 2.1

In ASP.NET Core 2.1 the methods changed slightly. The general method is similar to the 2.0, just the methods name and return types have been changed.

public static void Main(string[] args)
{
    CreateWebHostBuilder(args)
        .Build()
        .Migrate();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    return new WebHostBuilder()
        ...; // Do not call .Build() here
}

ASP.NET Core 2.0

如果您使用的是ASP.NET Core 2.0/EF Core 2.0,则进行了更改以更好地涵盖此类情况,以便命令行工具可以更好地工作.

ASP.NET Core 2.0

If you are on ASP.NET Core 2.0/EF Core 2.0 then there's been a change to better cover such cases, so that the command line tools can work better.

此公告中对此进行了很好的介绍.

It's pretty well covered in this announcement.

它归结为具有静态的BuildWebHost方法,该方法可配置整个应用程序,但不运行它.

It boils down to having a static BuildWebHost method which configures the whole application, but doesn't run it.

  public class Program
  {
      public static void Main(string[] args)
      {
          var host = BuildWebHost(args);

          host.Run();
      }

      // Tools will use this to get application services
      public static IWebHost BuildWebHost(string[] args) =>
          new WebHostBuilder()
              .UseKestrel()
              .UseContentRoot(Directory.GetCurrentDirectory())
              .UseIISIntegration()
              .UseStartup<Startup>()
              .Build();
  }

此外,对于EF 2.0,现在还建议在调用BuildWebHost之后将迁移移至main方法.例如

Also with EF 2.0 it's now recommended to move the migrations to the main method after BuildWebHost has been called. For example

    public static void Main(string[] args)
    {
        var host = BuildWebHost(args)
            .Migrate();

        host.Run();
    }

Migrate是扩展方法的地方:

public static IWebHost Migrate(this IWebHost webhost)
{
    using (var scope = webhost.Services.GetService<IServiceScopeFactory>().CreateScope())
    {
        using (var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>()) 
        {
            dbContext.Database.Migrate();
        }
    }
    return webhost;
}

现在,仅在执行应用程序时运行迁移.当您运行命令行工具时,将仅调用BuildWebHost且不应用任何迁移.

Now migrations only run, when your application is executed. When you run command line tools, only BuildWebHost will be called and no migrations applied.

这篇关于为什么删除迁移运行我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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