Deployment in Visual Studio 2012 RC Using Entity Framework Code First Migrations

In the Entity Framework version 4.3, Microsoft introduced Code First Migrations. Code First Migrations automates the process of making incremental changes to a data model and propagating those changes to the database. In earlier versions of Code First, you typically let the Entity Framework drop and re-create the database each time you changed the data model. This is not a problem in development because test data is easily re-created, but in production you usually want to update the database schema without dropping the database. The Migrations feature enables Code First to update the database without dropping and re-creating it. You can let Code First automatically decide how to make the required schema changes, or you can let Code First generate code that you can customize. For an introduction to Code First Migrations, see Code First Migrations.

Visual Studio 2012 RC can automate the process the of deploying a database that is managed by Code First Migrations. When you configure the publish profile, you select a check box that is labeled Apply Code First Migrations (runs on application start) :

Apply Code First Migrations check box

When you select this option, the deployment process automatically configures the application Web.config file on the destination server so that Code First uses the MigrateDatabaseToLatestVersion initializer class. The following illustration shows an example of what this setting looks like in the Web.config file:

MigrateDatabaseToLatestVersion initializer

The Apply Code First Migrations option also causes the deployment process to put an additional connection string in the destination Web.config file:

Database_Publish connection string

Code First uses this additional connection string for making the database schema changes. This connection string must specify user credentials that have administrative access to the database. Because Code First uses a separate connection string for schema updates, you can specify user credentials that have limited permissions in the application connection string. For more information, see Reviewing the Automatic Web.config Changes for Code First Migrations on the ASP.NET site.

Visual Studio does not do anything with the database during the deployment process. When the deployed application accesses the database for the first time after deployment, Code First automatically creates the database or updates the database schema to the latest version. If the application implements a Migrations Seed method, the method runs after the database is created or the schema is updated.

For more information about how to configure Code First Migrations deployment in Visual Studio 2012 RC, see How to: Deploy a Web Application Project Using One-Click Publish in Visual Studio and the tutorial series Deploying an ASP.NET Web Application to a Hosting Provider using Visual Studio or Visual Web Developer.

-- Tom Dykstra
ASP.NET and Web Tools Developer Content