Apply EF core migrations one by one in code

asp.net-core c# ef-core-2.0 entity-framework-core

Question

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?

1
4
3/13/2018 6:05:33 PM

Accepted Answer

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();
4
3/2/2018 6:29:52 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow