I am trying to figure out if there is a way to check which migration version that is the latest that has been running on a database. This to know the state of a certain database.
I read something about entity framework 6 is creating a table to hold track on it. I haven't tried it but I would like to know if theres anything similar to entity framework core.
It is possible to at least get the
migrations that are defined in the assembly but haven't been applied to the target database.
( source ) via
If you want the actual version of a database, there actually is a migration history table
__MigrationHistory containing a
MigrationId column which should give you what you want. Here's an article showing how to work with it: https://msdn.microsoft.com/en-us/data/dn456841.aspx
It is possible to get a list of pending migrations in Entity Framework Core using the following code:
var migrationsAssembly = db.GetService<IMigrationsAssembly>(); var historyRepository = db.GetService<IHistoryRepository>(); var all = migrationsAssembly.Migrations.Keys; var applied = historyRepository.GetAppliedMigrations().Select(r => r.MigrationId); var pending = all.Except(applied);
You need to include a couple of using statements:
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations;
There is an open issue for a more formal API similar to the one you have in EF 6: https://github.com/aspnet/EntityFramework/issues/577