Entity Framework  

How to Use Migrations in EF Core?

🌟 Introduction

When working with Entity Framework Core (EF Core), one of the most common tasks is keeping the database schema in sync with your application’s data model. This is where migrations come in. Migrations in EF Core allow you to update your database structure (tables, columns, relationships) whenever your data model changes, without losing existing data.

In this article, we will explain what migrations are in EF Core, why they are important, and how to use them step by step with examples in simple words. This guide is also SEO-friendly, so it will help anyone searching for how to use migrations in EF Core or EF Core migration commands.

🧰 What Are EF Core Migrations?

Migrations in EF Core are a way to apply changes in your C# data model classes to your database. Instead of manually editing the database, migrations generate scripts to add, remove, or modify database objects.

  • Purpose: Keeps your database schema in sync with your application model.

  • Benefit: Automates changes without deleting existing data.

  • Example Scenario: If you add a new property Age to your User class, you can run a migration to add the new Age column to the Users table in the database.

βš™οΈ How to Enable Migrations in EF Core

Before creating migrations, make sure EF Core tools are installed.

  1. Open the Package Manager Console in Visual Studio or use CLI (Command Line Interface).

  2. Install the EF Core tools package if not already done:

dotnet tool install --global dotnet-ef
  1. Add EF Core design package to your project:

dotnet add package Microsoft.EntityFrameworkCore.Design

This prepares your project to use migrations.

πŸ“ How to Create a Migration in EF Core

To create your first migration, run this command in your project folder:

dotnet ef migrations add InitialCreate
  • InitialCreate is just a name for your migration. You can choose any name.

  • This command generates migration files inside the Migrations folder.

πŸ“Œ Example

If your User class looks like this:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

After running dotnet ef migrations add InitialCreate, EF Core will create a migration to build a Users table with Id and Name columns.

πŸ—„οΈ How to Apply Migrations to the Database

Once you have created a migration, you need to apply it to the database using:

dotnet ef database update
  • This command updates the database based on the migration files.

  • Example: If you added a new column Age, running this command will add that column to the Users table.

πŸ”„ Updating a Migration in EF Core

When your data model changes, you can create another migration.

Example

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; } // new property
}

Now run

dotnet ef migrations add AddAgeToUser

This will generate a migration to add an Age column to the Users table. Then apply it with:

dotnet ef database update

🧹 How to Remove or Rollback a Migration

Sometimes you may need to undo or remove a migration.

  • To remove the last migration before applying it:

dotnet ef migrations remove
  • To rollback to a specific migration:

dotnet ef database update MigrationName

Example

dotnet ef database update InitialCreate

This command will rollback the database to the state after the InitialCreate migration.

πŸ“Š Benefits of Using EF Core Migrations

  • Keeps models and database in sync automatically.

  • Saves time by generating scripts instead of manual SQL changes.

  • Safe updates without losing data.

  • Version control friendly, as migrations can be committed with code.

  • Easier teamwork, since all developers can share the same migration history.

πŸ“ Summary

Migrations in EF Core are a powerful feature to keep your C# data models and database schema in sync. With simple commands like dotnet ef migrations add and dotnet ef database update, you can add new tables, modify columns, and manage changes without writing SQL manually. By learning how to use migrations in EF Core, developers can ensure smooth database updates and maintain consistency across projects.