Introduction
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.
Background
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.
Description
I have created a new Web Site project in Visual Studio 2015 RC and the project structure looks as below (left side):
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:
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.
I have changes the connection string to point to my LocalDB
database and renamed the database name as DatabaseMigrationEF7
as below:
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:
Let us create a model class named Book
having some properties as below:
public class Book
{
[Key]
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.
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:
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:
It seems that there is no error in the migration process. Let us check the database.
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.
I have not mentioned different DBContext
s, but as general migration framework will allow multiple database contexts. Hope you enjoy!
Points of Interest
- ASP.NET 5 and Entity Framework 7