Search code examples
asp.net-coreentity-framework-corewebdeploy

Webdeploy + EFCore migration : how to set target project


In our ASP.NET Core 2.1 solution, we have placed EF and migrations into a separate project.

It is fine for us manage migrations from default project thanks to CLI e.g :

dotnet ef migrations add NewMigration --project MyApp.MyBdd

In this example, we set MyApp.MyBdd as target project.

But the WebDeploy wizard doesn't shows any settings about Bdd migration.

I can manually set EF migration into pubxml file like this :

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 Script generation fails (nothing into logs)

Question: how to set target project to webdeploy settings file?


Solution

  • EDIT Thanks to @IvanStoev for pointing out that there is a way shown in the source code. Add this to your <PropertyGroup>:

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

    ORIGINAL ANSWER Unfortunately that's not possible. EFMigrations is used by a build task called 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, without any options to override it.

    I see two options: You could refactor your code so that your DbContext lives in the project you are deploying, or you could run the migrations not during deploy but on application startup, i.e. add

    dbContext.Database.Migrate();

    to your Startup.Configure() implementation.