RMD
RMD

Reputation: 3253

Enumerating Available Entity Framework Code First Migrations

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

Upvotes: 0

Views: 243

Answers (2)

user3411327
user3411327

Reputation: 1041

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();

Upvotes: 1

JC Ford
JC Ford

Reputation: 7066

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

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

Upvotes: 2

Related Questions