Imagine you are developing an application, but the database has not been created yet. You decide to use the Code First approach with Entity Framework Core. In this approach, you first create C# model classes that represent the tables in your database.
As your application grows, your model classes will also evolve. You may need to add new tables, add or remove columns, or modify existing ones. Making these changes manually in the database every time can be time-consuming and error-prone.
This is where Entity Framework Core Migrations become useful. Migrations provide a structured way to manage database schema changes as your application evolves.
When we start working with Entity Framework Core (EF Core), we often come across the term Migration. Let's understand what it means.
What Is Migration?
A Migration is a feature of Entity Framework Core that helps keep the database schema synchronized with your C# model classes.
Let's understand this with a simple example.
When using EF Core, we generally follow either the Code First approach or the Database First approach. The Migration feature is primarily used with the Code First approach.
In the Code First approach, we first create one or more C# model classes that represent database tables. We then ask EF Core to generate or update the database based on those model classes. This is where Migration comes into the picture.
Why Do We Need Migrations?
Let's understand this with the following example.
Example
Step 1: Create a Model Class
public class Employee
{
public int Employee_Id { get; set; }
public string Employee_Name { get; set; }
}
Step 2: Run Migration
Run the following command:
Add-Migration AddEmployeeTable
You can replace AddEmployeeTable with any meaningful name that describes the changes you are making.
When you run this command, EF Core compares your current model with the last applied migration (or an empty database if this is the first migration). It then generates a migration file containing the operations needed to update the database schema.
You can find the generated migration file in the Solution Explorer in Visual Studio. This file contains the C# code that defines the database changes to be applied.
For example, the generated migration may contain code similar to:
migrationBuilder.CreateTable(
name: "Employees",
columns: table => new
{
Employee_Id = table.Column<int>(),
Employee_Name = table.Column<string>()
});
Step 3: Apply the Migration
Run the following command:
Update-Database
The above command executes the generated migration script against the database. If the database does not exist, EF Core creates it.
In our example, the migration script contains instructions to create an Employees table with two columns:
How Migration Works
The migration process starts with creating or modifying C# model classes.
The Add-Migration command generates a migration file containing the required schema changes.
After reviewing the generated migration, the Update-Database command applies those changes to the database, ensuring that the database schema stays synchronized with the model classes.
The following diagram shows the typical workflow of Entity Framework Core Migrations:
![Flow Diagram of Entity Framework Core Migrations]()
If a Migration Already Contains the Code to Create, Delete, or Alter Database Tables, Why Do We Still Need to Run Update-Database?
Suppose you accidentally add a property to your model:
public string Salary { get; set; }
If EF Core automatically updated the database every time you modified a model class:
It could make unexpected changes to a production database.
It could break existing applications that depend on the current database schema.
It could introduce unwanted schema changes without giving you a chance to review them.
This is why EF Core separates the process into two steps:
Run the Add-Migration command to generate a migration file.
Review the generated migration and modify it if necessary.
Run the Update-Database command to apply the approved changes to the database.
This two-step approach gives developers full control over when and how database schema changes are applied.
Note: Always run the Add-Migration command first to generate the migration. After reviewing the generated migration, run Update-Database to apply those changes to the database.
Conclusion
In this article, we learned the basics of EF Core Migrations, including what they are, how they work, and how they help keep the database schema synchronized with the application model. There is much more to explore, which we'll cover in future articles.