MVC 4: Code First Migrations with SQL Server – Creating & Updating the DB Schema

Creating and keeping a database schema up to date using asp.Net & MVC 4 in Visual Studio (2010 in the below process) is quite easy and efficient.

Below are some basic steps to get you going.

Step 1. Create a local or remote SQL Server db (I’m using SQL Server 2008 Developer – locally) using SQL Management Studio. Don’t create any tables, etc., just the database.

Step 2. Update the web.config file in your root folder with a connection string for the database you just created. Below is mine:

 <connectionStrings>
    <add name="sampleDBContext" 
connectionString="Data Source=.\;Initial Catalog=sampleDB;Integrated Security=SSPI" 
providerName="System.Data.SqlClient" />
  </connectionStrings>

It’s important to have the connection string name match the name of the class of your DbSet, below is my dbcontext class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace SampleApp.Models
{
    public class sampleDBContext : DbContext
    {
        public DbSet<simpleTable> simpleTable{ get; set; }

    }
}

Step 3. In the Package Manager Console (in Visual Studio), run this command to enable migrations (at the “PM>” prompt):

Enable-Migrations -ContextTypeName SampleApp.Models.sampleDBContext

You should get this result:

Code First Migrations enabled for project SampleApp.

Step 4. At this point, the database hasn’t been updated. To update and create your tables, let’s first create a migration. In the Package Manager Console, run this command to create a migration (at the “PM>” prompt):

Add-Migration InitialMig

(If you were to open the .cs file just created in your migrations folder, you’ll see the create / update commands it will run.)

Step 5. Finally, to actually update the database, run:

Update-Database

(at the “PM>” prompt). You’ll see some messages like:

Applying code-based migrations: [201209122019596_InitialMig].
Applying code-based migration: 201209122019596_InitialMig.
Running Seed method.

Now if you look in at your database in SQL Management Studio, you’ll see your changes!

Going forward: go ahead and make changes to your models, you’ll just want to run the “Add-Migration” command, with a new migration name. ie: Add-Migration Mig2 and then run Update-Database to apply the changes. Simple!

MVC 4: Code First Migrations with SQL Server – Creating & Updating the DB Schema

7 thoughts on “MVC 4: Code First Migrations with SQL Server – Creating & Updating the DB Schema

  1. subseven12 says:

    Good Tutorial! But i’ve got an question about package manager console. Where do I find the package manager console in Visual Studio 2010? Or do I have too download it first? If so, where can I download it?

    thank you in advance

    Like

  2. khaled mahmod says:

    Thank you very much really you article helped me in my first MVC4 app
    for the master program in programming , cause am php developer and want to try ASP with MVC4 , thanks again from LIBYA – BENGHAZI
    and i hope to introduce critical subjects in future about LINQ or advanced MVC4 .

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s