Thursday 10 December 2015

Database Migration in Entity Framework 7

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 asDatabaseMigrationEF7 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 aDbSet 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 Booktable 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 DBContexts, but as general migration framework will allow multiple database contexts. Hope you enjoy!

1 comment:

  1. L ook… here I am I have tried out things in the same manner as you described and guess what!!! I have been successful in taking up the challenge and fulfilling the endeavor finally!!! There cannot be anything better than this.
    voyance serieuse gratuite par mail

    ReplyDelete

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...