Featured Article

Mastering .Net Core Entity Framework Migrations: A Complete Guide

Kenneth Jul 13, 2026

Embarking on a journey with .NET Core and Entity Framework (EF) often involves database migrations - a process that ensures your schema stays in sync with your EF models. This article delves into the intricacies of .NET Core Entity Framework migrations, a vital aspect of modern web development.

#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini
#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini

Migrations in .NET Core serve two primary purposes. Firstly, they create and update database schemas based on your EF Core models. Secondly, they allow you to apply and discard changes as needed, providing a level of flexibility and control over your database schema. Let's dive into how to effectively use EF Core migrations in your .NET Core projects.

Migration from ADO.Net to Entity Framework Core
Migration from ADO.Net to Entity Framework Core

Setting Up and Enabling Migrations

Before we begin, you need to ensure that your .NET Core project is set up correctly to use EF Core migrations. This involves installing the necessary NuGet packages and initializing your database context with a DbSet for each of your models.

Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?
Yardstick vs. .Net Core vs .NET Framework – Which One to Choose?

To start, create a new .NET Core console application and add the following packages: Microsoft.EntityFrameworkCore.SqlServer (or your desired database provider), Microsoft.EntityFrameworkCore.Design, and Microsoft.EntityFrameworkCore.Tools. These packages enable EF Core and migrations in your project.

Creating Your First Migration

Data Access in ASP.NET Core using EF Core (Database First)
Data Access in ASP.NET Core using EF Core (Database First)

With the necessary packages installed, you can now create your initial migration. In your Package Manager Console, run the following command: `Add-Migration InitialCreate`. This command generates a new migration class based on your DbContext, ready to be applied to your database.

The generated migration class contains the logic to create your database schema. You can review this code to ensure it matches your expectations. Once satisfied with the generated schema, you can apply the migration to your database with the `Update-Database` command.

Applying and Discarding Migrations

Online Courses - Learn Anything, On Your Schedule | Udemy
Online Courses - Learn Anything, On Your Schedule | Udemy

As your application evolves, you'll need to apply new migrations to keep your database schema up-to-date. To create a new migration, run: `Add-Migration YourMigrationName`. This command generates a new migration class with the changes needed to update your schema.

To apply a migration, use the `Update-Database` command. You can also discard the most recent migration using `Remove-Migration` or discard a specific migration using the migration name as an argument: `Remove-Migration MigrationName`. Always be cautious when discarding migrations, as this operation is irreversible.

Tracking Changes and Updating Your Models

.NET Core vs .NET Framework: What CTOs Must Know in 2026
.NET Core vs .NET Framework: What CTOs Must Know in 2026

EF Core automatically tracks changes to your models and generates migrations based on those changes. To manually trigger a migration, use the `Add-Migration` command with the `-o` option to specify the migration name: `Add-Migration YourMigrationName -o YourMigrationName.cs`.

EF Core supports three main types of changes: adding a new model, modifying an existing model, and removing a model. When you modify a model, EF Core updates the migration with the necessary schema changes. Be mindful that removing a model also removes its related data from the database.

Data Access in ASP.NET Core using EF Core (Code First)
Data Access in ASP.NET Core using EF Core (Code First)
Cloud Migration Architecture
Cloud Migration Architecture
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
#dotnet #entityframework #webapi #linq #dotnetcore #interviewpreparation #careergrowth #csharp #backenddevelopment | Shaheen Aziz
ASP.NET Core | Empty Web App to MVC App with Entity Framework core | Easy Migration
ASP.NET Core | Empty Web App to MVC App with Entity Framework core | Easy Migration
Data Center Migration Checklist for Enterprise IT Teams
Data Center Migration Checklist for Enterprise IT Teams
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Core First Approach of Entity Framework in ASP.NET Core MVC | Class 11
Free Entity Framework Book
Free Entity Framework Book
Build the back-end of a .NET 5 or .NET Core. 3.1 web application with Web API
Build the back-end of a .NET 5 or .NET Core. 3.1 web application with Web API
Moving to APIs doesn't mean overhauling everything overnight.

Our framework breaks it down: assess,
Moving to APIs doesn't mean overhauling everything overnight. Our framework breaks it down: assess,

Handling Multiple Databases and Connection Strings

In a production environment, you'll likely have multiple databases or connection strings for different purposes, such as development, testing, and production. EF Core allows you to manage these scenarios using DbContext constructors that accept a DbContextOptions builder.

You can create separate configuration files for each environment or use a conditional approach to set your connection string at runtime. By doing so, you can ensure that each environment uses the correct database and avoid accidentally applying migrations to the wrong database.

Mastering .NET Core Entity Framework migrations is an essential skill for any developer working with modern web applications. By understanding and effectively using migrations, you can streamline your development process and maintain a clean, up-to-date database schema. Happy coding!