Automatically perform migrations when publishing an ASP.NET Core application

Question

Is there a way I can automatically execute migration code (EF 7) when publishing an ASP 5 application to IIS using Web Deploy?

I tried

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

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

nobody worked for me.

Additional Information

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

After that, in the publication settings, I:

ASP 5 RC 1 Publish to IIS Using Web Deploy

Using Web Deployment in ASP 4 Applications I have additional database options:

ASP 4 Publish to IIS Using Web Deploy

+5
source share
3 answers

So, I added the -environment to my ef database command. Now it works:

 "postpublish": ["dnx ef database update -e Staging"] 

I have four different appsettings.json that contain different connection strings for each environment. You just need to specify the environment for the team to work.

+1
source

Use context .Database.Migrate ()

You can call this from your Startup class:

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

It will transfer your database to the latest version when the application starts. But be careful, perhaps comment out this code and disagree only when you want to complete your migrations.

+5
source

Apparently this process is not working. https://github.com/aspnet/Home/issues/622 After publication, you should find a script shell named "profile name" -publish.ps1. Then add your commands under these three lines near the end of this file. You might want to use powershell to make it easier to debug.

'Call Publishing-AspNet' | Write verbose

# call Publish-AspNet to perform the publish operation

Publish-AspNet -publishProperties $ publishProperties -packOutput $ packOutput

+1
source

Source: https://habr.com/ru/post/1242729/


All Articles