运行 独立应用程序中的迁移

Run migrations in self-contained applications

我已经使用 dotnet 创建了一个独立的应用程序:

dotnet publish -c release

部署包包含 .net core 运行times 本身 - 所以如果有人想使用应用程序 - 不需要单独安装 .net core 运行times。

但我的问题是...是否可以将 dotnet.exe 附加到部署包?

F.e。 - 我的网络应用程序需要迁移的数据库:

dotnet ef database update

此命令不能 运行 来自自包含应用程序(无法直接访问 dotnet.exe)。

这种情况下的场景是什么?我该如何处理?

解决方案是像那样将其添加到您的Startup.cs

public void Configure(
    IApplicationBuilder app,
    // ... various other parameters that you may have and below add the context that you want to run Migrations
    YourDbContext db1)
{
{
    // run the migrations before other code
    db1.Database.Migrate();
    // ...
}

PS。这有点奇怪,但是您将它作为额外参数添加到 Configure 方法签名中并且它起作用了(我猜它会自动实例化 dbContext 对象并调用该方法)。它对我有用。