Yet Another Tech Blog

Cool new database migration feature of EF 4.3


Recently, new version of Entity Framework was released (vesion 4.3). Out of many enhancements in EF, Database migration feature is the one most appealing. The database migration feature now allows developers to change the database schema in code first approach... That was even possible earlier.

But, this does not require to recreate database or re seed the data. Instead, EF will take care of all of this.


While using EF code first, it was very annoying that for a small change in schema we had to lose all the data we had created for test purpose. seeding was the option which looked more like a adjustment. What about the data which I had entered from the UI to create the different scenarios.

So, when I first heard about this new cool enhancement in EF, I jumped to see it working and to share it with all.

Article Body

Before looking at data migration feature, lets first create a simple model class and use data scaffolding to generate complete CRUD support. If you are unsure about this, you may want to start with Scott Gu's this excellent post.

Create a new MVC 3 project and name it as DataMigration. After project is created, add a new class in Model directory and name it as Employee.cs. The Employee.cs file would contain single model class named Employee having properties defined as EmployeeId, EmployeeName and Department.

The complete code of Employee.cs file is

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

namespace DataMigration.Models
    public class Employee
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string EmailAddress { get; set; }
        public string Department { get; set; }


    public class EmployeeContext : DbContext
        public DbSet<Employee> Employees { get; set; }

The EmployeeContext class is used to define data context.

Now, in solution explorer right click on Controllers folder and select Add-> Controller.

In the Add Controller dialogue, provide name of controller as EmployeesController and select Template as "Controller with read/write actions and views, using Entity framework". Then, select Model Class as Employee and Data Context class as EmployeeContext.

Click Add and this will create EmployeesController class with CRUD actions and corrosponding views.

Now run the App and navigate to /Employees... and there we have empty list of employees and "create new" link. Clicking on create new presents Create view.

Suppose we added couple of records like this.

All is good and there's nothing new. But what if now I want to add a Email address as a new field in the model class and in the database table. I can incorporate that in the model class like this.

public class Employee
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string EmailAddress { get; set; }
        public string Department { get; set; }


After making this change and running the application will produce error simillar to this.

The error clearly indicates that the schema of database has been changed and it needs to be dropped and recreated. This could be either resolved by deleting the database manually so that EF will recreate it again on next run or modifying the database schema manually so that it matches with model defined or by using setInitializer in Gloab.asax.

The EF 4.3 has a solution for this scenario.

To test this, lets create a new Project and first of all update the Entity Framework version using package manager console.

Open the package manager console (Nuget) and type in command "update-package EntityFramework"

This will remove old EF version and install latest one.

Then add the modal class Employee.cs again and add the same code in the class as above. Run the application and add the records in the employee model. We will now try to add one more column as EmailAddress in the Employee model. After running the application, the simillar exception is generated.

Now, to use database migration feature, type the command Enable-Migrations.

Now, the next step is to add migration. Use the command Add-Migration by passing any name which will be used by EF to name the migration file.

After add migration command is executed, the final task remaining is update the database as per the migration requirements. Run the command Update-Database.

This has clearly updated the database table. Now, to see the change in action, delete views containing Employee folder and also delete the EmployeeController class.

Generate the new EmployeeController again using scaffolding template. This will also create new view files which now includes new column EmailAddress. To check this, run the project and change the url to to Employee. The EmailAddress field can be seen to be added.

We now don't have to go for a "ugly" drop and create approach whenever a model changes a little. There are many other characteristics to this feature like default value for columns or adding a unique key values.

But this might require a another post (I will now try to play around with them :) )

Thanks for visiting my Site! comments are welcome.

Comments (1) -

  • Brian Noronha

    2/27/2012 9:44:01 AM |

    Interesting article...I really found the database/model updates a realy hassle in the earlier version of code-first, thats why i stuck to database first approach...

Comments are closed