Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy to production with entity framework code first

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();
            }
        }
    }
like image 809
Gmorken Avatar asked Jul 04 '13 07:07

Gmorken


People also ask

Is Entity Framework used in production?

Yes, we use it in production.

How do I publish my Entity Framework application?

Deploy the app to AzureIn Visual Studio, right-click the project in Solution Explorer and select Publish from the context menu. On the Pick a publish target page, choose App Service and then Select Existing, and then choose Publish.


1 Answers

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.

like image 139
Gábor Plesz Avatar answered Oct 17 '22 01:10

Gábor Plesz