dotnet / dotnet/efcore

Validate migration IDs

Open
#27,115 13 comments 0 reactions 0 assignees View on GitHub
area-migrations breaking-change customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### The bug
So in a project of mine we want to run migrations on startup. In the case of a failure in one of the migrations in startup we want to log which migration has failed and what has failed in the migration. So to achieve this i want to run each pending migration one by one, so if one fails i know which migration is failing and i can log this.

To achieve this i saw that the `IMigrator` offers the possibility to run migrations until a specified migration which is perfect. However when i tried this it throws the error "The migration was not found".

My code is extremely simple:

```C#
var migrator = dbContext.Database.GetService();
var pendingMigrations = dbContext.Database.GetPendingMigrations();
foreach(var pendingMigration in pendingMigrations)
{
try
{
migrator.Migrate(pendingMigration);
}
catch (Exception e)
{
var someException = e;
}
}
```
As you can see here i get the pending migrations from the Database object and then just want to run those individually. I also can see when debugging that in the `MigrationAssembly` within the migrator the migration name is there in the dictionary, so it seems very weird that this error is popping up.

Also when running `dbContext.Database.Migrate()` it works just fine

Here i have setup an example project of my exact scenario which is not working:
https://github.com/bramve-fenetre/DemoMigratorIssue

### Include provider and version information

EF Core version: Both 3.1 and 6.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: Both 3.1 and 6.0
Operating system: Windows 10
IDE: Visual Studio 2019

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.