Question

Is it possible to programmatically enumerate available code first migrations (EF 6+) for a particular context / assembly?

Specifically, I'd like to look at a particular assembly/context and list the available migrations as well as sort them by order in which they're applied.

It seems like the System.Data.Entity.Migrations.Infrastructure.MigrationAssembly is what I want, but it's internal. Short of using reflection to get at it, is there a better way?

EDIT

For an example application that looks at two different EF assemblies and gives you a "diff" of their migrations, see: EF Code First Migrations to Deploy Older Version

Was it helpful?

Solution

var config = new Configuration();
var migrator = new DbMigrator(config);

var all = migrator.GetLocalMigrations().ToList()
var applied = migrator.GetDatabaseMigrations().ToList();
var pending = migrator.GetPendingMigrations().ToList();

OTHER TIPS

Use the DbMigrator:

var config = new DbMigrationsConfiguration();
config.MigrationsAssembly = YourAssembly;
config.TargetDatabase = YourDb;
var migrator = new DbMigrator(config);
var local = migrator.GetLocalMigrations(); //all migrations
var pending = migrator.GetPendingMigrations();
var applied = migrator.GetDatabaseMigrations();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top