(EF Core) Getting Started with your first EF Core Migration

In the previous tutorial we added the EF Core migration tools to our project. We shall now run our first migration to create a blank database and its tables by the code first approach. After that we shall use the SQL Server Object explorer for verifying that our database and its tables have indeed been created.

Categories | About |     |  

Parveen,

Steps to Migration

Migration is a two-step process.

In the first step we use the Add-Migration command to auto-generate the source code files that contain all the necessary code for making alterations to the existing database.

This code is not yet executed.

In the second step the Update-Database command is used to execute the changes.

Let us get started!

Video Explanation

Please watch the following youtube video:

Step 1 - Add a Migration

Open the SQL Server Object Explorer.

It shows that our project has no database yet. So our first migration will create a blank database and its tables.

Let us now add a migration - the first migration - for creating our database and its schema tables.

Open the package manager console and type the command Add-Migration MyDBCreate

// the command is Add-Migration 
// the name of the migration is any 
// readable name such as MyDBCreate 

// adds a migration 
PM> Add-Migration MyDBCreate

// Package Manager Console 

This command adds a migration of a readable name MyDBCreate.

No database will get created yet!

Hit enter and allow it to completion.

It adds a folder called Migrations to the solution explorer. This folder is auto-created by the migrations tool. It will contain all our migrations - of the past, present and future.

As we can see, this folder contains a serially numbered source file of the same name as the migration that we added. The serial number is derived from the current system date and time. The purpose of this is to help keep the files sorted in a chronological order.

Let us have a look at the file.

This file contains a class of the same name as the migration. It is derived from a class Migration. It over-rides the Up method for forward migration.

In our case it creates a table.

The class also overrides the Down method. This method is for the rollback.

The migrations folder contains another source file named as snapshot. It contains a snapshot of the dbcontext that will result after this migration is applied.

The snapshot file will be used by the EF Core Migration tools in the future at the time of next migration. It will help decide the alterations on the basis of a comparison with this snapshot.

At this stage we can open the SQL Server Object Explorer to verify that no database has been created yet.

Step 2 - Execute the Migration

Now it is time to execute the migration!

Open the package manager console and type the command Update-Database

// the command is Add-Migration 
// the name of the migration is any 
// readable name such as MyDBCreate 

// adds a migration 
PM> Update-Database

// Package Manager Console 

Hit enter and allow it to completion.

After successful execution the database will be created along with its tables.

Refresh the SQL Server Object Explorer to verify that the InitialCatalog database db_blogs has been created.

This database has been created on the basis of the connection string we specified in the dbcontext class that we added in the previous tutorial.

A table called Blogs has also been created on the basis of our model.

This completes the tutorial on our first migration.


This Blog Post/Article "(EF Core) Getting Started with your first EF Core Migration" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.