Click here to Skip to main content
12,883,884 members (29,212 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 28 Jul 2014

Code First Migration in Entity Framework

, 28 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In this tip, we will discuss code first migration using Entity Framework


In our previous tip, we discussed what is code first approach and various database initialization strategies in code first approach. You can read them here.

In this tip, we will discuss database migration in code first approach. We know that, in code first approach at first we write the database code and according to code, it generated the database. This is very smooth and straight forward. Now, think about a situation where the database is generated using code first technique and then modification needed in database like need to add another column in some table and there is already existing data in that table. In this situation, we can implement code first approach database migration technique. In this example, at first we will create simple code first strategy to build database teen we will see how to migrate small change in database using command. Have a look at the below example:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
    public class Student
        public int studentId{get;set;}
        [MaxLength(20,ErrorMessage="Please enter name within 20 character")]
        public string studentName{get;set;}
        [MaxLength(20,ErrorMessage="Please enter name within 20 character")]
        public string studentSurname{get;set;}

        public int addressID { get; set; }

        public virtual Address Address { get; set; }
    public class Address
        public int addressId{get;set;}
        public string country{get;set;}
        public string state{get;set;}

        public string email { get; set; }

    public class MyDbContext : DbContext
        public MyDbContext() : base("name=DBConnectionString") 
        public DbSet<Student> Student { get; set; }
        public DbSet<Address> Address { get; set; }

There are two models defined called ”Student” and “Address” and “MyDbContext” class is defined which is inherited from DbContext class. Now, we will write the below code to generate the database for first time.

using(MyDbContext ctx = new MyDbContext())

Make sure you have provided proper connection string and once you open the database in SQL server management studio, you will find that the tables are created as per below screen.

Now, let’s think that we need to add another table in the existing database. How we will do that? We can use migration strategy to updated database as per new model definition. Now, we will add the below model to existing database.

    public class Course
        public Course()
            student = new List<Student>();
        public int CourseId { get; set; }

        public List<Student> student { get; set; }

        public string courseName { get; set; }

First of all, we have to Enable migration for this application. Open nuGet package manager console and run the below command:


Once this command runs successfully, we will see that one folder named “Migrations” has created in solution and it contains file named “Configuration”. This file contains the configuration section of migration like whether automatic migration is on or off and others.

Then we have to run.

add-migration “Identifier name”

Command which will add the migration history in another .cs file, the migration name is nothing but string identifier which may need to rollback some migration in future.

Here, we are seeing that the .cs file is created with unique identifier name followed by our specified migration identifier separated by underscore(_).

If we open the file, we will see code something like below for this migration example. The code is purely auto generated and this is nothing but for migration history.

public partial class CourseAdded : DbMigration
        public override void Up()
                c => new
                        studentId = c.Int(nullable: false, identity: true),
                        studentName = c.String(nullable: false, maxLength: 20),
                        studentSurname = c.String(nullable: false, maxLength: 20),
                        addressID = c.Int(nullable: false),
                        courseID = c.Int(nullable: false),
                .PrimaryKey(t => t.studentId)
                .ForeignKey("dbo.Course", t => t.courseID, cascadeDelete: true)
                .ForeignKey("dbo.Address", t => t.addressID, cascadeDelete: true)
                .Index(t => t.courseID)
                .Index(t => t.addressID);
                c => new
                        CourseId = c.Int(nullable: false, identity: true),
                        courseName = c.String(),
                .PrimaryKey(t => t.CourseId);
                c => new
                        addressId = c.Int(nullable: false, identity: true),
                        country = c.String(),
                        state = c.String(),
                        email = c.String(),
                        pin = c.String(),
                .PrimaryKey(t => t.addressId);            
        public override void Down()
            DropIndex("dbo.Student", new[] { "addressID" });
            DropIndex("dbo.Student", new[] { "courseID" });
            DropForeignKey("dbo.Student", "addressID", "dbo.Address");
            DropForeignKey("dbo.Student", "courseID", "dbo.Course");

The beauty of the migration history is, at any point of time, we can run the migration code and bring our database in this state. Now, we want to reflect the updated model into database and for that we have to run the below command.


Here, I have run the latest migration history to seed database.

Let’s open the database and we should find another table in database just like below.

Border Line

Database migration is absolutely useful when the database already exists and it’s meaningful when database exists along with data. Entity Framework will just reflect the updated model to database without altering the existing data.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sourav Kayal
Software Developer DELL International
India India
I am software developer from INDIA. Beside my day to day development work, i like to learn new technologies to update myself. I am passionate blogger and author in various technical community including , and codeproject. My area of interest is modern web technology in Microsoft stack. Visit to my personal blog here.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
diponsust30-Jul-14 22:53
memberdiponsust30-Jul-14 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 28 Jul 2014
Article Copyright 2014 by Sourav Kayal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid