12

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?

3
  • Interesting, I have no answer for you, but would be really curious to hear what use case you have for needing this. Mar 2, 2018 at 16:12
  • 1
    im applying data updates associated with each migration so i need each data update to be applied just after the migration Mar 2, 2018 at 16:32
  • ah, right, ok, I could certainly see that. Mar 2, 2018 at 16:36

1 Answer 1

15

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();
1
  • 2
    This is really useful for installer or updater tools that keep migration logic out of the main application. May 27, 2022 at 16:56

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.