Lesson 19 of 40 Database Intermediate 35 min

Database Migrations

Learn how database migrations work in Entity Framework Core and why they are essential for creating and updating your database schema in a controlled, organized way.

Part 1: What Is a Migration?

A migration is a way to describe changes to your database structure over time. Instead of manually creating tables and columns in SQL Server, Entity Framework Core can generate these changes for you based on your model classes.

This means that if you change your model, EF Core can help keep the database in sync.

Part 2: Why Migrations Matter

As your application grows, your data model will change. You may add new properties, remove unused ones, or create new entities entirely.

Migrations are important because they:

Part 3: A Simple Example

Suppose your Student model originally looks like this:

public class Student
{
  public int Id { get; set; }
  public string Name { get; set; }
  public string Course { get; set; }
}

Later, you decide to add an Email property:

public class Student
{
  public int Id { get; set; }
  public string Name { get; set; }
  public string Course { get; set; }
  public string Email { get; set; }
}

EF Core can detect this change and create a migration to add the new Email column to the database.

Part 4: Creating a Migration

To create a migration, you normally use the Package Manager Console or command line tools. In Visual Studio, a common command is:

Add-Migration InitialCreate

This creates a migration file that represents the database structure for your current models.

If you later make more changes, you create another migration with a different name, such as:

Add-Migration AddStudentEmail

Part 5: Applying a Migration

After creating a migration, you apply it to the database using:

Update-Database

This tells EF Core to execute the migration and update the database schema.

If the database does not exist yet, EF Core can create it. If it already exists, EF Core applies only the necessary changes.

Part 6: What EF Core Generates

When you create a migration, EF Core generates files that contain instructions for the database changes. These files are usually stored in a Migrations folder.

Each migration describes:

This gives your database schema a history, similar to version control for code.

Part 7: Migrations in the Student Project

In your Student CRUD application, migrations make it easy to evolve the database as the project grows. For example:

Instead of rebuilding the whole database manually, migrations apply only the required updates.

Part 8: Best Practices

These habits make your project easier to maintain, especially as it becomes larger or more collaborative.

Summary

Database migrations are one of the most useful features of Entity Framework Core. They help your ASP.NET Core application evolve safely by keeping the database structure synchronized with your model classes. Once you understand migrations, managing schema changes becomes far more organized and reliable.

VISUAL STUDIO 2026 MADE EASY
Recommended Book

VISUAL STUDIO 2026 MADE EASY

Build real applications with C#, VB.NET, Python, JavaScript, C++, and .NET 10. A practical companion for mastering Visual Studio 2026 step by step.