(EF Core) Migration on Alterations to a Model

A real life project evolves over a period of time. This process necessitates additions and alterations to the model classes - properties are added, properties are removed, foreign key relations are established, modified, and even removed. All these changes require a matching change to the database structure. This is a challenging task especially when a project is live and contains critical data that must be preserved and protected against corruption and damage. In this tutorial we learn how the EF Core Migration Tools help us easily achieve migrations without writing a single SQL statement or query.

Categories | About |     |  

Parveen,

Recap of the Previous Tutorial

Let us recall what we have done so far.

We are working on a simple project of one model class called Blog. We executed our first migration and created a database and a table on the basis of this model class.

You can have a look at the SQL Object Explorer to confirm that there is a table blog consisting of two columns called id and heading. These columns correspond to two properties on the model class.

Video Explanation

Please watch the following youtube video:

Step 1 - Add a Property to the Model

Let us now modify our model by adding a property called DateOfPublish.

public class Blog
{


  // already existing property 
  [Key]
  public int ID { get; set; }

  // already existing property 
  public String? Heading { get; set; }

  // -----added now------ 
  public DateTime DateOfPublish { get; set; }
}

Add a property of DateTime data type.

So, now, our model has three properties but the database has columns for only two of them. There is no column corresponding to the newly added property. The model and the database are out of sync now.

Our task now is to use the ef migration tools to add a new column to the current database. The database is in use, it is in production - so we have to rule out the possibility of deleting it and recreating it from blank.

Step 2 - Add a Migration

As we already know, an ef core based migration is a 2-step process.

As our first step let us run the Add-Migration command. We can give any readable name to this migration, such as MyColAddition.

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

PM> Add-Migration MyColAddition

// package manager console 

Hit enter to execute this command. Allow it to run to completion.

We can verify the completion by opening the solution explorer. The migrations folder now contains a new source file for this migration.

Next let us execute the migration.

Step 3 - Execute the Migration

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

PM> Update-Database

// package manager console 

Hit enter to execute the command and allow it to completion.

That's it! We didn't have to write a single line of SQL code. Everything was handled by the ef core migration tools.

Step 4 - Verification on SQL Server Object Explorer

Next let us open the SQL Server Object Explorer to verify that the blog table now contains the additional column.

As you can see, a column for dateofpublish has indeed appeared in the table.

We demonstrated a simple migration in this tutorial. But the steps remain the same even for the most complex changes to the underlying model classes. In the next tutorial we shall see how to obtain the underlying SQL scripts and so that they can be fine tuned with the help of database experts and running them directly.


This Blog Post/Article "(EF Core) Migration on Alterations to a Model" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.