Entity Framework for Beginners Part 2 – Migrations

This post is part 2 of 4 covering Entity Framework Code-First for Beginners.

  1. Part 1 – Setting Up Entity Framework Code-First for Beginners
  2. Part 2 – Migrations in Entity Framework Code-First
  3. Part 3 – DbContext
  4. Part 4 – Entity Framework Code-First Extras

In this post, I will take a close look at Entity Framework migrations. Migrations are a way to get changes in the model of your code into your database. Migrations can also seed data, or get some default data into your database. Let’s get started.

Why use Entity Framework migrations?

It’s a good question. You will have to decide if it is worth it to you after reading this article but don’t do yourself a disservice and ignore it. We have all been there. We update our code and forget to make the changes to the database. Using migrations means that you don’t have to worry about that. If you do it right, it is pretty seamless.

How to use Entity Framework migrations?

This is a step-by-step process. I am providing a sample project here. Download it and open it in Visual Studio 2017. From this point on, I will assume you are using this project. The database specified in this project uses SQL Server Express and will be located in the App_Data folder.

If you run the application, nothing really happens at this point. Its just the boilerplate ASP.NET MVC application. We have a model class, but, since we aren’t using the DbContext yet, nothing changes.

The first thing we need to do is enable migrations. In the Tools menu, hover over the NuGet Package Manager option and select Package Manager Console. This will open the NuGet console, probably in the Tools tabs at the bottom of your screen. In the package manager console, enter the following command:

enable-migrations

The enable-migrations command will give us a new folder in the project named Migrations. In the Migrations folder is a new class named Configuration. The Configuration class has a method named Seed.

Seeding Data

Seed allows us to insert default data into our database. This will allow us to maintain data between updates to the database performed by migrations. Copy the code below into the Migrations\Configuration.cs file to populate the Contacts table with some default data.

context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Harry", HomePhone = "555-555-5555", ID = 1, IsActive = true, LastName = "Bosch", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Dalinar", HomePhone = "555-555-5555", ID = 2, IsActive = true, LastName = "Kholin", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Mat", HomePhone = "555-555-5555", ID = 3, IsActive = true, LastName = "Cauthon", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Eddard", HomePhone = "555-555-5555", ID = 4, IsActive = true, LastName = "Stark", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Albus", HomePhone = "555-555-5555", ID = 5, IsActive = true, LastName = "Dumbledore", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Bruce", HomePhone = "555-555-5555", ID = 6, IsActive = true, LastName = "Wayne", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Elayne", HomePhone = "555-555-5555", ID = 7, IsActive = true, LastName = "Trakand", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "George", HomePhone = "555-555-5555", ID = 8, IsActive = true, LastName = "Smiley", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Jack", HomePhone = "555-555-5555", ID = 9, IsActive = true, LastName = "Ryan", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Locke", HomePhone = "555-555-5555", ID = 10, IsActive = true, LastName = "Lamora", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Jamie", HomePhone = "555-555-5555", ID = 11, IsActive = true, LastName = "Lannister", MobilePhone = "555-555-5556" });
context.Contacts.AddOrUpdate<Contact>(new Contact { FirstName = "Mitch", HomePhone = "555-555-5555", ID = 12, IsActive = true, LastName = "Rapp", MobilePhone = "555-555-5556" });

Also, in the Configuration.cs file, change the Configuration constructor to be AutomaticMigrationsEnabled = true;

Now, we need to go back to the NuGet Package Manager Console. Type in the command update-database

The update database command will insert our seed data but does a bit more that we will get into next.

Schema Changes

We can also use Entity Framework Migrations to update our database schema when our model classes change. Take another look at the Contacts model class. Entity Framework is a convention-based framework. Look at the ID column. You will see that it hasn’t been decorated with the [Key] data annotation attribute but if you look at the database table, the ID column is indeed our primary key and it is also an Identity column. This is because ID is, by Entity Framework convention, an identity and primary key column if no other primary key is defined in the model class.

The AddOrUpdate commands we are issuing in the Configuration.cs file will attempt to update existing data if it finds a matching primary key column. Otherwise, it will insert a new record. Now, in the Contact.cs model class, add a string property named EmailAddress.

public string EmailAddress { get; set; }

Because it is a string property, we don’t have to worry about nulls right now. We just want the field added.

So, back in the NuGet Package Manager Console window, run the update-databasecommand again. If you look at the Contacts table definition, you should see the EmailAddress field present.

Conclusion

Well, that is a basic introduction to Entity Framework migrations. I have hit on most of the stuff you simply have to know to be effective with migrations. I hope you have enjoyed this part of the series and will see you in the next installment when we look at how to use Entity Framework. The finished product can be downloaded from here.

Comments 3

  1. Pingback: Entity Framework for Beginners Part 4 – Extras | Edumacate Blog

  2. Pingback: Entity Framework for Beginners Part 3 – DbContext | Edumacate Blog

  3. Pingback: Entity Framework Code First for Beginners Part 1 – Setup | Edumacate Blog

Leave a Reply

Your email address will not be published.