Entity Framework Core Migraton

Entity Framework Core 1 Migrations

Entity Framework Core

It's been some while I'm following the news and blogs about Entity Framework Core 1 (or formerly Entity Framework 7.0). The new Entity Framework is written totally from scratch. It is not the next version of Entity Framework. In fact, Entity Framework Core is a completely new product. So it was a really good decision from the team that they renamed the product to Core 1 and removed the confusion.

It is not the next version, but a completely new product called Entity Framework Core.

Here I'm going to talk about what I found new in Entity Framework Core 1 Migrations.

Entity Framework Migrations

Do you remember how database snapshots was storing the Entity Framework 6? It is changed completely in Core 1.

The Ef 6 way

I really like the way that migrations work in EF 6. But there was something really annoying there. The last snapshot of database was stored in a resource file per each migration like 201509090914163_Init.resx. The last snapshot was encoded in someway and stored in Target

It was designed to be used internally, so it was really hard to see what's the last migration unless you go and check the database, or you decode the that resource. At the time I posted a question in StackOverflow about how to decode this string. You can find the difficult answer there!

Entity Framework Core 1 Migrations

I found this concept in Entity Framework Core 1 very clean and straightforward. I really enjoy from the way it is designed. There is no snapshot per each migration anymore, instead, there is one snapshot per context like:

[ContextType(typeof(MyContext))]
public class MyContextModelSnapshot : ModelSnapshot
{
    public override IModel Model
    {
        get
        {
            var builder = new BasicModelBuilder();
            builder.Entity("Student", b =>
            {
                b.Property<int>("Id");
                b.Property<string>("FirstName");
                b.Property<string>("LastName");
                b.ForRelational().Table("Students");
             });
                
             return builder.Model;
            }
        }
    }
}

This is a great representation for snapshots. because of these reasons:

  • It is totally human readable, or better to say: developer readable!
  • As it is located in a .cs file like MyDbContextModelSnapshot.cs  not a .resx file you can easily compare the different versions of the snapshot in the source control history.
  • In some rare cases (which I don't recommend!) you can edit this snapshot in a more managed way.

Considering this great improvement to migrations scenario, I think migrations is super cool in Entity Framework Core 1.0. 

If you're interested to learn more about migrations in Entity Framework Core 1, you can find out some more details in this great post from Brice. He is a software engineer on the Entity Framework team at Microsoft.

About the author

Mehran Davoudi

A former technology geek! Currently a mentor and software architect, working as a consultant for companies with large scale software development.
Why dvd? Read about me (uɒɹɥəɯ)!

View all posts

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *