6

I'm coming from asp.net Web Forms switching to MVC and entity framework code first approach. I have a question. How can I set up my environment to deploy to production?

I'm using Visual Studio 2012 and deploys a web deploy package. Locally I have SQL Express and in production, I have SQL Server 2008.

What I want is to develop an test locally on my pc and from time to time deploy my solution to production with a web deploy package. I don't want to run migrations in the production system, instead, I want to generate scripts from visual studio which I then can append to production SQL.

I tried to:

  • Create Initial migration in dev.
  • Update database locally
  • Generate script, update-database -script -sourcemigration:InitialCreate
  • Apply this script in production
  • Deploy the application to production

Is this the correct approach? What about my migrations that will run locally, will they not in production to when I deploy because of my migrations code?

In global.asax

Migrator.RunMigrations();

where RunMigrations is a static method in a custom Migrator class like this

public class Migrator
    {
        public static void RunMigrations()
        {
            var migrator = new Configuration();
            var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(migrator);
            if (dbMigrator.GetPendingMigrations().Any())
            {
                dbMigrator.Update();
            }
        }
    }

2 Answers 2

2

You can with Web Setup Project (for the installation an MSI Custom Action is needed):

The actual work of updating the database is can be done by the migrate.exe tool. To make the MSI package run it properly turned out to be a bit of a challenge. I first included migrate.exe in the installation package to have it deployed to the bin directory together with the assemblies of the system. There is support for running a .exe file as a custom action in the web setup projects. Unfortunately, I couldn’t get migrate.exe to work unless the working directory was set to the bin directory. The working directory for custom actions is c:\windows\system32 by default. To handle that, a small vb-script was used.

http://coding.abel.nu/2012/04/update-database-msi-custom-action/

UPDATE:

I found this, this and this, maybe it will help.

Sign up to request clarification or add additional context in comments.

Comments

0

Rollback all migrations

update-database -TargetMigration:0

update-database -Script

https://cpratt.co/migrating-production-database-with-entity-framework-code-first/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.