You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggestion: programmatically update to a specific migration #9968
I can already use PowerShell to update to a specific migration:
Update-Database -Migration ExampleMigration
I can also programmatically apply all pending migrations using the Migrate() extension method:
dbContext.Database.Migrate();
It would be nice if there was an overload of Migrate() that took a specific migration to update to. Right now this is not possible without digging into infrastructure classes like Migrator that aren't meant for public use.
Use cases:
Automated testing/validation of migration Up/Down code.
Abstracting database migration details away from QA/IT who don't know/don't care about EF.
Reacted by Evgeniy Mamchenko, Graeme Miller, Dániel Leiszen, pascaljr, Jannik Buschke, anaximander23, Dustin and Ryan Bonomo
@jessebarocio Could you explain a bit more about what the situation is when you don't want the database updated to the latest migration, given that this is the one that matches the model as currently built by the context?
@ajcvickers sure, here are the two use cases I came up with.
I want to run an integration test that rolls back all migrations and then updates to the latest migration. This makes sure team members haven't broken the migration path (by, for example, tweaking code in a migration's Up() or Down() methods). If a developer does break migrations it can be a pain to fix. Running this as an integration test during CI builds would make detection easier.
The other use case I thought of is more specific to my organization. Right now in projects that don't use EF our QA department runs a utility to rollback/update the database to a specific version so they have a clean set of test data to work with. It would be nice to have a way to create this same type of utility for projects that do use EF.
Use the constant Migration.InitialDatabase to revert all migrations.
Reacted by Evgeniy Mamchenko, joshcomley, Graeme Miller, D. St-Jacques, Dias Khan, Antonio Ortiz Pola, Matthias Langhard, Mathieu, Michael Mickelson, Kevin Winkel and 15 more
Triage: since there is a way to do this without using internal surface already and because we think that adding it to the top-level API surface may cause confusion/additional unneeded decision points, we have decided to not to this. However, we will add a note to the docs for people who do need this low-level control: dotnet/EntityFramework.Docs#518
Reacted by Mathieu, Peter-Optiway, Melvin Ray Herr, Dustin and Ryan Bonomo
I ran into another use case that might be valid (required by my application needs):
Using 2 DBContexts in a code-first migration strategy requires "synchronized/inter-wined" up-migration calls (i.e.: migrate DbContext1 up to version A, then DBContext2 up to version A', then DBContext1 again up to version B, etc.).
"Could you explain a bit more about what the situation is when you don't want the database updated to the latest migration, given that this is the one that matches the model as currently built by the context?"
For example we need to execute upgrade process.
Current app version is D.
If user has app A version we need to upgrade app from A to B: upgrade DB model from A to B, process data migration from A to B.
After that from B to C: upgrade DB model from B to C, process data migration from B to C.
After that from C to D: upgrade DB model from C to D, process data migration from C to D.
Activity
ajcvickers commentedon Oct 9, 2017
@jessebarocio Could you explain a bit more about what the situation is when you don't want the database updated to the latest migration, given that this is the one that matches the model as currently built by the context?
jessebarocio commentedon Oct 9, 2017
@ajcvickers sure, here are the two use cases I came up with.
I want to run an integration test that rolls back all migrations and then updates to the latest migration. This makes sure team members haven't broken the migration path (by, for example, tweaking code in a migration's
Up()orDown()methods). If a developer does break migrations it can be a pain to fix. Running this as an integration test during CI builds would make detection easier.The other use case I thought of is more specific to my organization. Right now in projects that don't use EF our QA department runs a utility to rollback/update the database to a specific version so they have a clean set of test data to work with. It would be nice to have a way to create this same type of utility for projects that do use EF.
bricelam commentedon Oct 9, 2017
Workaround
As mentioned, you can do this by dropping down to some lower-level components.
Use the constant
Migration.InitialDatabaseto revert all migrations.ajcvickers commentedon Oct 11, 2017
Triage: since there is a way to do this without using internal surface already and because we think that adding it to the top-level API surface may cause confusion/additional unneeded decision points, we have decided to not to this. However, we will add a note to the docs for people who do need this low-level control: dotnet/EntityFramework.Docs#518
afust003 commentedon Jun 18, 2021
I ran into another use case that might be valid (required by my application needs):
Using 2 DBContexts in a code-first migration strategy requires "synchronized/inter-wined" up-migration calls (i.e.: migrate DbContext1 up to version A, then DBContext2 up to version A', then DBContext1 again up to version B, etc.).
MaximMikhisor commentedon Oct 25, 2021
"Could you explain a bit more about what the situation is when you don't want the database updated to the latest migration, given that this is the one that matches the model as currently built by the context?"
For example we need to execute upgrade process.
Current app version is D.
If user has app A version we need to upgrade app from A to B: upgrade DB model from A to B, process data migration from A to B.
After that from B to C: upgrade DB model from B to C, process data migration from B to C.
After that from C to D: upgrade DB model from C to D, process data migration from C to D.