Entity Framework Core Migrations Using ASP.NET Core

Problem

How to use Entity Framework Core migrations in ASP.NET Core project.

Solution

This is a continuation of the previous post on creating CRUD with ASP.NET Core and EF Core.

To utilise EF migrations, add NuGet package to ASP.NET Core Web Application project: Microsoft.EntityFrameworkCore.Design

Add CLI tools by editing .csproj file:

Here is a list of .NET CLI commands relating to EF migrations:


NOTE
: you’ll need to run CLI commands from within ASP.NET Core Web Application project however, point to the project containing DbContext and Entities using -p option (for add and remove commands).

Discussion

Below are examples of most commonly used commands.

To add migration:

To remove migration:

To update database with all pending migrations:

Trying to remove migration that has already been applied:

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.EF.Crud

Leave a Reply