Search code examples
entity-frameworkasp.net-coremsbuildasp.net-core-mvcwebdeploy

Why aren't my migrations automatically being executed when I publish to staging using web deploy?


Background

I've built a website that I created, starting with the .Net Core 2.0 web application template which now has 2 projects and 1 data context:

  • data project containing both identity and website data models
  • web project containing the MVCs

I have a publish profile that I use and when I publish it to staging, the migrations are processed as part of publishing which works well for me as the website application and schema is upgraded in one go.

More recently, I've built a 2nd website starting with the .Net Core 2.1 web application template which has 3 projects and 2 data contexts

  • data project including website data models
  • data identity project including identity
  • web project containing the MVCs

Issue

I have created a staging profile for the 2nd website which looks the same to me as the 1st one but the migrations don't automatically get processed when I publish.

Looking at the build logs, when I publish the 1st website it includes a section for generating entity framework SQL scripts:

Generating Entity framework SQL Scripts...
Executing command: dotnet ef migrations script --idempotent --output "D:\Dev\dotnet\Core\foo1\Web\obj\Staging\netcoreapp2.1\win-x64\PubTmp\EFSQLScripts\Data.ApplicationDbContext.sql" --context Data.ApplicationDbContext 
Generating Entity framework SQL Scripts completed successfully
Adding database (data source=VMFoo;initial catalog=com.foo1-staging;trusted_connection=True;multipleactiveresultsets=true)
Updating file (com.foo1.staging\Data.dll).
Updating file (com.foo1.staging\Data.pdb).
Updating file (com.foo1.staging\web.config).
Updating file (com.foo1.staging\Web.deps.json).
Updating file (com.foo1.staging\Web.dll).
Updating file (com.foo1.staging\Web.pdb).
Updating file (com.foo1.staging\Web.runtimeconfig.json).
Updating file (com.foo1.staging\Web.Views.dll).
Updating file (com.foo1.staging\Web.Views.pdb).
Adding database (data source=VMFoo;initial catalog=com.foo1-staging;trusted_connection=True;multipleactiveresultsets=true)
Adding database (sitemanifest/dbFullSql[@path='data source=VMFoo;initial catalog=com.foo1-staging;trusted_connection=True;multipleactiveresultsets=true']/sqlScript)
Publish Succeeded.

However, when I publish with the 2nd website, it doesn't:

Updating file (com.foo2.staging\web.config).
Updating file (com.foo2.staging\Web.deps.json).
Updating file (com.foo2.staging\Web.runtimeconfig.json).
Publish Succeeded.

I've increased the logging level around the build task (Tools -> Options -> Projects and Solutions -> Build and Run -> MSBuild project build output verbosity) in VS2017 and I've tracked it down to the following line on the solution that doesn't call the GenerateEFSQLScripts task:

Task "GenerateEFSQLScripts" skipped, due to false condition; ('$(_IsAspNetCoreProject)' == 'true' And '$(IsGenerateEFSQLScriptsDisabled)' != 'true' And @(EfMigrations) != '') was evaluated as ('true' == 'true' And '' != 'true' And  != '').

The log from the working website says:

Using "GenerateEFSQLScripts" task from assembly "C:\Program Files\dotnet\sdk\2.1.301\Sdks\Microsoft.NET.Sdk.Publish\build\netstandard1.0\..\..\tools\net46\Microsoft.NET.Sdk.Publish.Tasks.dll".
Generating Entity framework SQL Scripts...

It looks as though the issue is to do with @(EfMigrations)? If so, can anyone shed any light on where this gets picked up from and how I can get migrations processed as part of publishing?

When I look at the web.sourcemanifest.xml file in the PubTmp folder there's an extra line:

<dbfullsql path="D:\Dev\dotnet\Core\foo1\Web\obj\Staging\netcoreapp2.1\win-x64\PubTmp\EFSQLScripts\Data.ApplicationDbContext.sql" />

Of course, this could be purely because the GenerateEFSQLScripts task didn't run but I thought I'd flag this up incase it helps someone answer my question.


Solution

  • My particular issue was to do with having the appsettings.json file in a config folder instead of the root of the project.

    Tooling works on the assumption that the appsettings.json is always in the root.

    One solution worked because I originally had the appsettings.json in the root, performed a publish but then later moved it to a config folder.

    Microsoft are going to look to add the ability to configure this so tooling will allow a custom location for appsettings.json.

    More details around this can be found on Git:

    Where is @(EfMigrations) sourced from?