Is it a way to check which migration(version) that currently is on a database

.net entity-framework-core

Question

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.

1
5
11/20/2017 10:47:23 AM

Accepted Answer

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 DbMigrators Method getPendingMigrations().

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

5
11/9/2016 2:16:35 PM

Popular Answer

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

See https://github.com/aspnet/EntityFramework/issues/6110#issuecomment-242220554.

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



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