Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically execute migrations when publishing ASP.NET Core app

Question

Is there any ways that I can automatically execute the migration code (EF 7) when publishing my ASP 5 application to IIS using Web Deploy?

I Tried

  • in the project.json, I added this code in the scripts:

    "scripts" : { "prepublish": ["dnx ef database update", "other commands..."], "postpublish": ["dnx ef database update"] }

none worked for me.

Additional Info

I followed the instructions on this link to deploy my ASP 5 RC-1 web application to IIS using web deploy.

After doing so in the publish settings I have:

ASP 5 RC 1 publish to IIS using Web Deploy

Using web deploy in ASP 4 applications I have additional database options:

ASP 4 publish to IIS using Web Deploy

like image 915
A-Sharabiani Avatar asked Feb 10 '16 19:02

A-Sharabiani


2 Answers

Use context.Database.Migrate()

You can call this from your Startup class:

using (var context = new MyContext(...))
{
    context.Database.Migrate();
}

It will migrate your database to the latest version on application startup. But be careful doing it, maybe comment out this code and uncommend only when you want to run your migrations.

like image 142
Andrei Avatar answered Sep 23 '22 14:09

Andrei


Apparently this process does not work now. https://github.com/aspnet/Home/issues/622 After you publish you should find the power shell script with the name of "profile name"-publish.ps1. Then add your commands below these three lines close to the end of this file. You might want to use powershell to make it easier to debug.

'Calling Publish-AspNet' | Write-Verbose

# call Publish-AspNet to perform the publish operation

Publish-AspNet -publishProperties $publishProperties -packOutput $packOutput

like image 22
Sergey Barskiy Avatar answered Sep 20 '22 14:09

Sergey Barskiy