animalito maquina
animalito maquina

Reputation: 2404

Apply EF core migrations one by one in code

I need to apply Entity Framework Core migrations one by one in code, i can call the await dbContext.Database.MigrateAsync(); but it applies all the pending migrations in one block.

Is there an extension or other method to apply the migrations just one by one or selecting the name of the migration to apply?

Upvotes: 12

Views: 2939

Answers (1)

animalito maquina
animalito maquina

Reputation: 2404

There is a IMigrator service with a Migrate(string targetMigration) method that receives the migration name, this method is used by the Migrate() extension. From a DbContext instance it can be used as:

await dbContext.Database.GetInfrastructure().GetService<IMigrator>().MigrateAsync(targetMigrationName);

The pending migrations names can be queried by this extension:

var pending = dbContext.Database.GetPendingMigrations();

Upvotes: 15

Related Questions