为什么要删除迁移 运行 我的应用程序?
why remove-migration run my app?
我在上个月进行了一些版本升级,现在我注意到当我使用 "remove-migration" 删除我还原的迁移时,首先是 运行 我的应用程序。
(我注意到因为我们在启动时更新数据库,所以我遇到了无法删除迁移的情况,因为每次我尝试删除迁移 - 它会自动 运行 适用的启动迁移到数据库,然后删除失败,因为它在数据库中看到它。)
有什么想法吗?
ASP.NET 核心 2.1
更新
在 ASP.NET Core 2.1 中,方法略有变化。大体方法与2.0类似,只是方法名称和return类型有所改变。
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核心2.0
如果您使用的是 ASP.NET Core 2.0/EF Core 2.0,那么我们将进行更改以更好地涵盖此类情况,以便命令行工具可以更好地工作。
this announcement 中对此进行了很好的介绍。
它归结为有一个配置整个应用程序的静态 BuildWebHost
方法,但 运行 它没有。
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 方法。例如
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
而不会应用任何迁移。
我在上个月进行了一些版本升级,现在我注意到当我使用 "remove-migration" 删除我还原的迁移时,首先是 运行 我的应用程序。
(我注意到因为我们在启动时更新数据库,所以我遇到了无法删除迁移的情况,因为每次我尝试删除迁移 - 它会自动 运行 适用的启动迁移到数据库,然后删除失败,因为它在数据库中看到它。)
有什么想法吗?
ASP.NET 核心 2.1
更新在 ASP.NET Core 2.1 中,方法略有变化。大体方法与2.0类似,只是方法名称和return类型有所改变。
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核心2.0
如果您使用的是 ASP.NET Core 2.0/EF Core 2.0,那么我们将进行更改以更好地涵盖此类情况,以便命令行工具可以更好地工作。
this announcement 中对此进行了很好的介绍。
它归结为有一个配置整个应用程序的静态 BuildWebHost
方法,但 运行 它没有。
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 方法。例如
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
而不会应用任何迁移。