Overview of Entity Framework Code First Migrations with example


Entity Framework code first approach allows to create POCO classes and generates or changes your new or existing database. Entity Framework has full control over the data table generation and modification using Database Initializers and Migration. Migration is a very useful concept to handle all type of Database schema changes. Using Migrations you can easily add, update, delete new or existing database.


Developers always face challenges during development and during development we have to change our existing model classes or add new model classes, which may cause a mismatch schemas with existing database. In this article we are going to see database initializers and various features of Entity Framework code first migrations with examples.

please refer this articles before proceeding next.

Understand Database Initializers

Entity Framework code first approach create database automatically based on POCO classes. By default Database Initializers will handle this behavior. Entity Framework has basic four Database Initializers given below.

- Default Database Initializer class unless you declare another class. As the name suggests it will create new Database if it doesn't exists.

- This Database Initializer class will drop and recreate the whole database whenever any data model changes found. This class is useful during starting phase of development when you don't have any concern with existing database records.

- This class will always delete your existing database and re-create new database every time application runs. This Initializer useful when you want fresh database every time.

- This Database initializers added in Entity Framework version 6.0. This initializer is used to update the database to the latest version. This is very useful initializer to update your database scheme without deleting your database records.

You can code your custom database initializer class if you wish to go beyond  inbuilt database initializer classes.

Where to declare Database Initializers
The most basic approach is to declare it in your DbContext class constructor.
public class StudentDbContext : DbContext
    public StudentDbContext ()
         : base("StudentDbContext")
       Database.SetInitializer(new DropCreateDatabaseWhenModelChanges());
    public DbSet<Student> Students { get; set; }
    public DbSet<Department> Departments { get; set; }


If you think why Code First migrations are so important, even if we have inbuilt Database Initializers? You can read section Understanding Database Initializers which all have minor or major disadvantages. Migrations play important role when the data model changes and existing database records both are important for us. Migration can modify your existing database schema without deleting any existing records. There are two types of migrations (1) Automatic and (2) Code base or Manual. In this article we are discussing Code base or Manual Migrations.

Enabling Migrations

To enable Migrations in your project go to Tools > Library Package Manager > Package Manager Console
Note : Select project where DataContext file located.
type below command

When we fire Enable-Migrations command it will automatically check DataContext class is corresponding to any existing database or not. This command will add "Migrations" folder with Configuration class and InitialCreate migration class in the project.

Note:  You can set automatic migration through writing command Enable-Migrations -EnableAutomaticMigrations

enable migration in entity framework code first approach

Configuration class
You can add the configuration level setting in constructor so it will automatically initialize when configuration class instance generate. AutomaticMigrationsEnabled = false will prevent from automatic migration every time when the application runs. you can add another property AutomaticMigrationDataLossAllowed = false, this will prevent existing data loss. Seed method used to insert default values in datatable. EF by default write comment code for sample instruction.

InitialCreate migration class
Each migration class has two methods Up() and Down(). EF code first gives you good flexibility to update or change Up and Down methods codes until migration is not run. When we run migrations, Up method will call by default and update the database based on code written in its scope. Down method exactly opposite of Up method. Down method will use when we want to revert our migrations. In our example Up method is creating tables Department and Student whether in Down method, it is dropping all tables and removing all constraints.
Enable Migration command will automatically add new table __MigrationsHistory in the database. MigrationId is unique and exactly the same name as your migration name in application. ContextKey will indicate appropriate Context Model.
migrations in entity framework code first approach

Add migrations

please read our previous article [^], We used the same example to perform migration operations. Now add Country field in Student.cs class and add a new migration in the project

write below command in Package Manager Console to add migration
PM> Add migrations AddCountryToStudent

it will create new migration class in your Migrations folder.
using System.Data.Entity.Migrations;
namespace MVCEFCodeFirstApp.Migrations
    public partial class AddCountryToStudent : DbMigration
        public override void Up()
            AddColumn("dbo.Student", "Country", c => c.String());
        public override void Down()
            DropColumn("dbo.Student", "Country");

Update Database

When we are adding new migration don't forgot to update database for sync. To update the database write below code in Package Manager Console. -verbose is used when you want to get SQL query.
PM>Update-Database -Verbose

after execute update-database command it will sync and update database.

Reference links

If you have any concerns, post as a comment.


Popular posts from this blog

12 opensource and free asp.net MVC gridview contols

Scheduling background jobs using Hangfire in ASP.NET Core

Difference ExecuteNonQuery vs ExecuteScalar vs ExecuteReader in asp.net