Web Deploy + EF Core 迁移:如何设置目标项目

Webdeploy + EFCore migration : how to set target project

在我们的 ASP.NET Core 2.1 解决方案中,我们将 EF 和迁移放到了一个单独的项目中。

感谢 CLI,我们可以管理默认项目的迁移 例如:

dotnet ef migrations add NewMigration --project MyApp.MyBdd

在本例中,我们将MyApp.MyBdd设置为目标项目。

但是 WebDeploy 向导没有显示任何关于 Bdd 迁移的设置。

我可以像这样手动设置 EF 迁移到 pubxml 文件:

Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
       <TimeStampOfAssociatedLegacyPublishXmlFile />
       <EncryptedPassword />
  </PropertyGroup>
  <ItemGroup>
      <EFMigrations Include="MyApp.MyBdd.MyDbContext">
         <Value>Server=MyServer%3bDatabase=MyBdd%3bIntegrated Security=True%3b</Value>
      </EFMigrations>
  </ItemGroup>
</Project>

Entity Framework SQL 脚本生成失败(日志中没有任何内容)

问题:如何将目标项目设置为webdeploy设置文件?

编辑 感谢@IvanStoev 指出 源代码中显示的一种方式。将此添加到您的 <PropertyGroup>:

<EFMigrationsAdditionalArgs>
     --project MyApp.MyBdd
</EFMigrationsAdditionalArgs>

原始答案 不幸的是,这是不可能的。 EFMigrations 由名为 GenerateEFSQLScripts 的构建任务使用(see source) which always uses the project being deployed for migrations, as identified by the MSBuildProjectDirectory variable. You can see here that it's that project being run here,没有任何选项可以覆盖它。

我看到两个选项:您可以重构您的代码,以便您的 DbContext 存在于您正在部署的项目中,或者您可以 运行 迁移不是在部署期间而是在应用程序启动时进行,即添加

dbContext.Database.Migrate();

您的 Startup.Configure() 实施。