Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

Entity Framework (EF) is an object-relational database mapper that enables easy database access for .NET developers using domain-specific .NET objects that EF can generate for you. As we saw in the first blog post on Entity Framework, you can use EF to create your database from the classes you have defined in your project code. You can add or remove properties from your classes and have EF migrate the changes into your database as well.

As developers, you often face projects where the specifications change over time for many reasons. Sometimes the client isn’t sure exactly what they need or their needs change during the course of the project. The design and analysis may determine that you need to add fields to the database or even remove fields from the database for one reason or another. For example, you may have new requirements to keep track of a user’s email address. Or you may need to add and track a user’s cell phone. A field/column entitled fName may even need to be renamed to firstName, or maybe you need to split a name into separate fields. There are many reasons that database fields/columns may need to change on a given project.

For this blog post, we are going to revisit the Visual Studio project used in The .NET Entity Framework Code First post. If you haven’t created that project, follow along with this post, but we recommend viewing that sample code.

If you open the project from The .NET Entity Framework Code First, you will see the following classes:

And our database looked like this:

Now, for the sake of our discussion, let’s say that we already have run our application at least once so our database has already been created, and we have decided that we want to add the keynote speaker’s name to the database. We can do that by just adding it as a property in our SeminarID class and then getting a DBA to write the necessary SQL. If you were allowed, you could write the SQL to add the field/column to the database table. That would just modify the database, however, and that is not the best approach since you may need to change it later. The best approach is to automate tracking and manage the migrations as the project grows. To do that we will use Code First Migrations.

First, you need to enable Code First Migrations using the Package Manager Console. If it isn’t already open, select Tools->Library Package Manager->Package Manager Console. Now run the command shown below. Notice that we used the Namespace of our application and the name of our DbContext class to create a fully qualified context:

Visual Studio will process the command and you should get a result that looks something like this:

You will also get a new Migrations folder added to your project that contains two files.

Configuration.cs contains the settings that Migrations uses to do its magic work. We won’t touch it for this blog post, but in a future post we will show you how to use it to provide seed data for your database. The other file is named <timestamp>_IntialCreate.cs and contains the code shown here:

Closer examination shows that it contains the code to take you from an empty database to one that contains the tables for the Seminar and Topic classes defined in our code.

Now that we have enabled Migrations and the basics are all in place, let’s modify our Seminar class to add a key note speaker so that our Seminar class now looks like this:

Next, we need to run migrations again using the command Add-Migration AddSpeaker. You can give this any name you want, but it is good to make it descriptive of the changes. Add-Migration will check for changes between the model classes and the last migration that was run, createing a new migration. You will notice that it is called <timestamp>_AddSpeaker.cs, and it has been added to the Migrations folder in our project. The timestamp part of the name is what Migrations uses to keep changes in order to track which changes have been applied. Now we need to run Update-Database so that any pending changes get applied to the database. I like to use the –Verbose switch so that I can see what is going on.

Now if you refresh the database in Server Explorer you will see that your changes have taken place and the new field/column have been added to the Seminars table.

We can go on and on like this, adding and dropping properties from our model classes and running the Add-Migration and Update-Database commands, as our database changes over the life cycle of our application. We still need to manually add code to the application to collect the user’s input or display the data to the user, but as you have seen, all of the hard work in our project has been taken care of for us by the Entity Framework and Code First Migrations.

Safari Books Online has the content you need

Check out these .NET Entity Framework books available from Safari Books Online:

Programming Entity Framework: Code First acts as an extension to Programming Entity Framework 2nd Edition. Code First is an additional means of building a model to be used with the Entity Framework and is creating a lot of excitement in the .NET development community. The book begins with an overview of what Code First is, why it was created, how it fits into the Entity Framework and when to use it over the alternatives.
Entity Framework 4 in Action is an introduction to the Entity Framework for developers working in .NET who possess a working knowledge of ADO.NET. Beginning with a review of the core ideas behind the Object Relational Management (ORM) model, it outlines how the Entity Framework offers a smooth transition from a traditional ADO.NET approach. Featuring numerous examples and in-depth case studies, the book explores the Entity Framework story in clear, easy-to-follow detail.
Entity Framework 4.1: Expert’s Cookbook holds many examples to help guide you through tough technical decisions and avoid technical landmines. The book will guide you from just using Entity Framework to enhancing the data access wizard. You will be guided through complex mapping scenarios, query definition, reusability, integration with other technologies, and architectural management. The approach is step-by-step and test driven so that it is focused as much as possible on solving problems and getting the most out of the time spent working through the book.

About the author

Marshall Harrison is the founder of GotSpeech.Net and as well as the owner and CEO of GotSpeech Consulting LLC. He holds a B.S. degree in Computer Science and was a Microsoft MVP from 2006 – 2011. He is currently focusing on Microsoft Lync as well as Windows 8 and Windows Phone 8 devleopment. He can be reached through his blog at

Tags: .NET Entity Framework, database, migrations, Package Manager Console, Visual Studio,

Comments are closed.