0

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

Community
  • 1
  • 1
RMD
  • 3,421
  • 7
  • 39
  • 85

2 Answers2

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

var all = migrator.GetLocalMigrations().ToList()
var applied = migrator.GetDatabaseMigrations().ToList();
var pending = migrator.GetPendingMigrations().ToList();
JC Ford
  • 6,946
  • 3
  • 25
  • 34
  • Ah, so this returns the name of the migration - including the timestamp, which is what allows me to order them properly. Thanks! – RMD Mar 31 '14 at 16:59
1

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();
user3411327
  • 1,031
  • 8
  • 14