Entity Framework migrations have been around for some time. They are particularly useful if you are using Entity Framework code-first migrations. Migrations recognise changes in your Entity Framework pattern and script those changes. You can then perform those changes against your database.

What is Code-First?

Code-first is when you build up your entities inside your .NET application and then script those changes to the database. The opposite to this is database-first. Database-first allows you to create your entities in the database, and bring them in to your .NET application.

Code-first has become the popular choice in recent times. Database-first displays a database diagram of your relationships. This can require a very large database diagram on more complicated database models in the form of an EDMX file.

How to Setup Migrations

This article assumes you have already set up your database context. It also assumes you have assigned your entities inside your database context as database sets and configured your connection string against your database context.

Follow this useful article on setting up your database context if you need assistance.

The NuGet Package Manager Console will be required for this. To find this, open up Visual Studio. Thereafter, go to Tools, NuGet Package Manager and Package Manager Console in VS 2019.

.NET Framework applications will need migrations enabled. Run the following command in NuGet Package Manager:

Enable-Migrations

A Migrations folder will be created inside the project that contains your database context. The Migrations folder will store any database changes that you make.

Migrations are already enabled by default in .NET Core applications.

Add Migrations

We want to sync our database context to our database. The first thing to do is to add a migration by running this command in NuGet Package Manager:

Add-Migration Initial

By the way, “Initial” is the name we’ve given our migration. Every time we add a migration, we have to create a unique name for it. This will create our migration script that will look similar to this:

// 201912081428466_Initial.cs
namespace EFMigrations.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.FilmEntities",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.FilmEntities");
        }
    }
}

However, if the NuGet Package Manager cannot find the Add-Migration command, you will have to install Microsoft.EntityFrameworkCore.Tools library. You can do so by running this command in NuGet Package Manager:

Install-Package Microsoft.EntityFrameworkCore.Tools

Assuming everything has gone to plan, the script is produced in the form of a class. There is an Up and Down method in the class. The Up method scripts the changes that are going to be made to the database. The Down method scripts the changes in the event of a rollback.

A model snapshot file has also been created. This file creates a copy of what has been scripted against your database context.

What if I made a mistake?

Oh dear! Well if you need to revert a migration, you can run the following command in NuGet Package Manager. This will remove your last migration.

Remove-Migration

This will only work if you haven’t applied your migration to the database. If you have, it will show you an error message similar to that.

Update Database

Assuming you are happy with these changes, you can run the following command in NuGet Package Manager:

Update-Database

These changes should now have been implemented into your database. Your database will also have created a dbo.__EFMigrationsHistory table. Inside there, it contains the Migration Id and the Product Version.

Continuing with Migrations

You need to run the “Add-Migration” and “Update-Database” every time you wish to sync your database context with your database. It will use your database context and your model snapshot file to determine what changes it needs to make to the database.

About David Grace

David Grace

I am a .NET developer, building web applications in .NET Framework and .NET Core with a SQL Server database.

Some of the .NET packages I have used include Entity Framework and MVC.

I've also used many JavaScript frameworks such as React and jQuery, and have experience building CSS in SASS.

Twitter Feed