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?
Migrations are enabled by default in EF Core. They are managed by executing commands. If you have Visual Studio, you can use the Package Manager Console (PMC) to manage migrations. Alternatively, you can use a command line tool to execute Entity Framework CLI commands to create a migration.
Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.
One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With