How to rollback EF 7 migrations programmatically?

c# database-migration entity-framework entity-framework-core unit-testing

Question

I would like to write a unit test which migrates to the latest schema then tears back down to nothing.

For the migration I can do :

db.Database.Migrate();

but how to I rollback?

1
2
12/7/2015 8:49:48 AM

Accepted Answer

The Commands use something like this;

var migrator = db.GetInfrastructure().GetRequiredService<IMigrator>();
migrator.Migrate("Migration1");

Where Migration1 is the name of the migration. This suggests you can stick "0" in there to roll them all back.

4
9/27/2016 4:14:06 PM

Popular Answer

Was digging in the assemblies and found I can do this :

db.Database.GetService<IMigrator>().Migrate("0");


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