MVC4 SQL migrations step by step

In my earlier post , I showed how to use code first with the server where the app cant create a database.

Now, we are looking to migrate our schema on to the server. It would be nice if it we can get a nice DDL script for this that we can walk through the test envrironments to production. Our DBA friends will definitely allow this.

Let's see how to do this step by step.

Step 1: Open the Package Manager Console (Tools=> Library Package Manager => Package Manager Console)

Step 2: run Update-Package EntityFramework
This updates the EF to 4.3.1 version.

Step 3: Restart vs after the update (important!)

Step 4: run Enable-Migrations -EnableAutomaticMigrations

It generates Migrations folder with Configuration.cs class. It looks like:

namespace MvcApplication4.Migrations
{ using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<MvcApplication4.Models.CompStoreDBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(MvcApplication4.Models.CompStoreDBContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

}

Step 5: Build the project (important!) as we have added new stuff in the project. If we dont build, the up and down method are empty. It will get clearer after next step

Step 6: run Add-Migration "InitialCreate"
It creates a file with a time stamp such as 201205261630323_InitialCreate.cs. This file looks like below:

namespace MvcApplication4.Migrations
{ using System.Data.Entity.Migrations;

public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "Computers",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Descr = c.String(),
                    Type = c.String(),
                })
            .PrimaryKey(t => t.Id);

    }

    public override void Down()
    {
        DropTable("Computers");
    }
}

}

This file has Up and down methods. The up method is for applying the change and the down is there if we need to roll back. It also helps in managing the destructive db changes such as changing the datatype of decimal column to int.

Step 7: run Update-Database -Verbose
This is going to work only if you have ddl permissions on the db. If not, the next step shows a way to create a DDL script. Note : You cant create another migration untill the previous ones are applied.

Step 8: run Update-Database -Script -SourceMigration:$InitialDatabase -TargetMigration:"InitialOne"It generates a nice looking script for our model that we can take to production.

Hope this helps.. Again, dont forget to build after the Enabling the migration otherwise up and down methods get created empty!..

Update:

I used VS 2010 for this demo. If you use VS 2012, building the project after the enabling the migrations is not required.



comments powered by Disqus