6

I've got several projects using Asp.Net Core 1.0 and Entity Framework Core 1.1.0

I have a code first migration approach and I publish to Azure through Visual Studio 2015.

The way I used to apply migrations in Azure Sql Server was by enabling a checkbox on publish: "Entity Framework Migrations - Apply this migration on publish", where I typed in a connection string.

I updated several packages and now for one of my projects I don't see this option to apply migrations on publish anymore. I can see that it tries to discover data contexts but it does not find anything (although it's there in the same project..)

See below:

  • Project where I can apply migrations on Azure when publishing: apply migrations available

  • Project where the option to apply migrations on Azure when publishing is gone: apply migrations not available

I suspect it has something to do with some dependency version for the project and not with my IDE because I am using the same Visual Studio (2015 update 3) for both projects.

I couldn't find information about this. What dependency allows this option? If I find out what version is problematic then the remaining question is how to apply migrations when publishing then?

Both projects have the migrations in the Web project and both projects are using "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0"

UPDATE 1: I managed to find what's involved in this. It seems that if I use these dependencies:

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"

Visual studio is able to find data context and offer the option to Apply migrations on publish. But if I use newer versions of those dependencies like:

"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.Design": "1.1.0"

Then that option to apply migrations is gone and VS cannot find any data context when publishing.

I'd need to find out what's the latest status with the migrations thing and Asp.Net Core.

diegosasw
  • 13,734
  • 16
  • 95
  • 159
  • 1
    Just out of curiosity: What's the problem with calling `dbcontext.Database.MigrateAsync()` during application startup? When you deploy it to azure and you are using deployment slots it would perform deploy the app and then ping it once to make sure its warmed up before swapping it with the production slot – Tseng Dec 22 '16 at 22:02
  • 2
    There are some debates around this. Personally I like to have the migrations applied before the application is deployed so that if the migration failed to apply there wouldn't be any issue with the current app and DB in production. While if it's in the code as part of the Startup that means that the app will have to be deployed first and then the migration will be applied. If that migration fails to apply then the app would stop working and would have to re-deploy the previous version. But I could consider using `dbcontext.Database.MigrateAsync()` if there are good arguments to do so? – diegosasw Dec 22 '16 at 22:21
  • Well, that's why I mention Azure's deployment slots: learn.microsoft.com/en-us/azure/app-service-web/web-sites-staged-publishing See https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-staged-publishing (especially "Swap with preview (multi-phase swap)" part). You'd basically publish the app in the staging slot, it you could trigger a warm up (call some url which forces IIS to start your application) and then start switching the two. I think you can also set it up to only change if the url called for warmup was successful, but don't know it for sure right now – Tseng Dec 22 '16 at 22:32
  • Not all the plans in Azure have multiple deployment slots.. At the moment I am on the basic one so that's not an option for me. But yes, it sounds good for the plans that allow that. – diegosasw Dec 22 '16 at 22:40
  • I found [this](http://stackoverflow.com/questions/35324156/automatically-execute-migrations-when-publishing-asp-net-core-app) related. You need to to add `"postpublish": ["dnx ef database update -e Staging"]` in `project.json` – Kos Dec 23 '16 at 07:08
  • Solution did not work for me in VS2017 asp.net core 1.1 Ended up adding the following line to the csproj file, in the DotNetCliToolReference section. – Dustin Gamester May 08 '17 at 20:25

2 Answers2

3

I have stumbled upon a very similar problem (Publishing fails to look for data contexts within the project to publish) within Visual Studio 2017 after creating an empty ASP.NET Core Web API project and adding migrations.

In order to make it work, I have included the following lines within the .csproj file:

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

To work with migrations, you can use the Package Manager Console (PMC) or the command-line interface (CLI) When using PMC, EF migrations are not discovered during the publish. By using CLI, the EF migrations section appears.

Kelly Orr
  • 751
  • 8
  • 17
Bbone
  • 1
  • 1