Click here to Skip to main content
15,612,734 members
Articles / Entity Framework
Posted 6 May 2015


12 bookmarked

Database Migration in Entity Framework 7

Rate me:
Please Sign up or sign in to vote.
4.33/5 (12 votes)
6 May 2015CPOL3 min read
How to migrate database in Entity Framework 7 and Visual Studio 2015


I will demonstrate database migration using Entity Framework 7 and Visual Studio 2015 RC. I will use DNX (.NET Execution Environment) commands from the prompt to do the migration.


New Visual Studion 2015 RC provides many new excellent features for developing applications targeting different platforms. It also introduces new .NET Execution Environment (DNX) which replaces the .NET Runtime (CLR) which has some astounding features to work with applications and data.

Please visit this link for latest features for ASP.NET 5.


I have created a new Web Site project in Visual Studio 2015 RC and the project structure looks as below (left side):

Image 1Image 2

On the right side, I have explored the references list which are the default items that are being using now. There are two lists which have been renamed from ASP.NET 5.0 and ASP.NET 5.0 Core from previous CTP releases.

There are so many features of this RC are to discuss, but in this tip, I will focus only on the database migration.

To start with the migration, please read the following article to install DNVM to Windows machine:

For the migration, there is a package or library for this which you find in the project.json file (with other packages).

"EntityFramework.Commands": "7.0.0-beta4"

project.json file for the project as shown below:

Image 3

To run the commands from command window, we need to define the alias for the code generation package using "commands" section in the project.json file.

"commands": {
      "ef":  "EntityFramework.Commands"

Here the string "ef" can be any string as you wish to call it from the command prompt.

With this setup, let us look at the existing connection string for the database when we have created the project.

Image 4

I have changes the connection string to point to my LocalDB database and renamed the database name as DatabaseMigrationEF7 as below:

Image 5

Before we proceed, let us check a couple of things:

Here is my LocalDB server in my machine which does not have the DatabaseMigrationEF7 in the list of dab:

Image 6

Let us create a model class named Book having some properties as below:

public class Book
       public int Id { get; set; }
       public string Title { get; set; }
       public string Description { get; set; }
       public string Author { get; set; }
       public DateTime DatePublished { get; set; }
       public string Type { get; set; }

As I will be using the existing default DatabaseContext which is ApplicationDbContext, we need to add a DbSet object for our model so that the table is created with the other default tables for Identity Management.

To accomplish this, I have added the DbSet object as below:

public DbSet<Book> Books { get; set; }

Having done this, let us open the command prompt and move to the project folder where our project.json file resides.

Image 7

I have run the command...

dnx . ef migration add newBook 

In Visual Studio 2015 RTM with beta7 the migration command has been slitely changed:

dnx  ef migrations add newBook



...which resulted in the following screen:

Image 8

After that to update the database, I have applied...

dnx . ef migration apply

Updated command in Visual Studio 2015 RTM is:

dnx ef database update


...and in result, I have got the following screen:

Image 9

It seems that there is no error in the migration process. Let us check the database.

Image 10

We can see now the database DatabaseMigrationEF7 has been created and the list of tables where Book table is also there.

If we explore the Book Table, we can verify the list of properties that we have defined in our model.

Image 11

I have not mentioned different DBContexts, but as general migration framework will allow multiple database contexts. Hope you enjoy!

Points of Interest

  • ASP.NET 5 and Entity Framework 7


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionThanks!! Pin
Member 1182599821-Oct-15 1:56
Member 1182599821-Oct-15 1:56 
QuestionHow about DB initialization/seeding with EF7 Pin
Shimmy Weitzhandler6-Sep-15 18:31
Shimmy Weitzhandler6-Sep-15 18:31 
AnswerRe: How about DB initialization/seeding with EF7 Pin
Mostafa Asaduzzaman7-Sep-15 10:50
Mostafa Asaduzzaman7-Sep-15 10:50 
GeneralRe: How about DB initialization/seeding with EF7 Pin
Shimmy Weitzhandler9-Sep-15 0:52
Shimmy Weitzhandler9-Sep-15 0:52 
QuestionThanks Pin
ramyajaya1-Sep-15 4:44
ramyajaya1-Sep-15 4:44 
Questionmissing step Pin
dustinin20-Jul-15 11:24
dustinin20-Jul-15 11:24 
AnswerRe: missing step Pin
Mostafa Asaduzzaman20-Jul-15 11:35
Mostafa Asaduzzaman20-Jul-15 11:35 
QuestionGreat Article Pin
Cyclone5528-May-15 20:31
Cyclone5528-May-15 20:31 
AnswerRe: Great Article Pin
Mostafa Asaduzzaman28-May-15 20:34
Mostafa Asaduzzaman28-May-15 20:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.