Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4, Migrations - How to run 'update-database' on a production server

I can use package manager to run 'update-database -verbose' locally.

Probably a stupid question but I can't find it online - once my website is deployed - how can I run this manually on the server?

Secondarily - what other strategies would you recommend for deploying database migrations to production - and how would they be preferable?

Thanks

like image 518
niico Avatar asked Jun 10 '13 09:06

niico


3 Answers

You have a couple of options:

  • You could use update-database -script to generate the SQL commands to update the database on the server
  • You could use the migrate.exe executable file that resides in the package folder on /packages/EntityFramework5.0.0/tools/migrate.exe. I've used it successfully in the past with Jet Brains' Team City Build Server to setup the migrations with my deploy scripts.
  • If you're using IIS Web Deploy you can tell the server to perform the migrations after publish (see pic below)
  • You could setup automatic migrations, but I prefer to be in control of when things happen :)

Update: Also, check out Sayed Ibrahim's blog, he works on the MsBuild Team at Microsoft and has some great insights on deployments

enter image description here

like image 73
amhed Avatar answered Nov 17 '22 23:11

amhed


I know that the question is already answered, but for future reference:

One of the options is to put something like this in the constructor of your DB context class:

public MyDbContext()
    {
        System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());            
    }
like image 10
Martin Falc Avatar answered Nov 17 '22 21:11

Martin Falc


For us, the DBAs are the only group to have access to the production (and pre-production) environments. We simply use the Update-Database -Script package console command to get the Sql required to update the database. This gets handed off to them where they can validate it, etc.

Maybe a little too simplistic for some but it works.

HTH.

like image 4
Sean Kenny Avatar answered Nov 17 '22 23:11

Sean Kenny