Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run migrations on azure for dotnet core site deployed with GitHub

I have a small website built with .Net core that includes a SQLite database and entity framework core. I'm using VS Code and on a mac.

It is easy to manage the database locally - dotnet ef database update works great. The problem is running migrations when deploying to Azure.

My repo is on GitHub, and I configured Azure to pull code from GitHub when I push to the master branch. The deploy is working fine, but migrations aren't running on Azure.

I've seen some suggestions that I can use yourDbContext.Database.Migrate() in Startup.cs, however it appears that .Migrate() is no longer available.

I've tried downloading the deployment script from Azure and customizing it by adding call :ExecuteCmd dotnet ef database update -e Production to deploy.cmd, but that doesn't appear to be working.

I've tried using the PS shell I can access through the Kudu site to manually run migrations, but when trying to run dotnet ef database update the result is, No executable found matching command "dotnet-ef"

There is a very similar question here (EF Core (1.0.0) Migrations On Azure App Services), but that question did not get any answers.

like image 676
Dingels35 Avatar asked Oct 21 '16 13:10

Dingels35


People also ask

How do I enable auto migration in .NET core?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations –EnableAutomaticMigration:$true command (make sure that the default project is the project where your context class is).

How do I run EF migrations on deploy?

Right click your web project, click publish, use web deploy, go to your databases, target your new database, ensure Execute Code First Migrations is checked (this will run all the migrations you've done for your localdb on your new database).

How do I migrate to .NET core?

Adding a Migration So, firstly, you need to create a migration. Open the Package Manager Console from the menu Tools -> NuGet Package Manager -> Package Manager Console in Visual Studio and execute the following command to add a migration. If you are using dotnet Command Line Interface, execute the following command.


1 Answers

You can run context.Database.Migrate() when you initialize the database in your code. The migrations will run when you first launch your application

like image 199
matthijsb Avatar answered Sep 21 '22 03:09

matthijsb