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.
Over time, a business may migrate from an existing database to save costs, enhance reliability, achieve scalability, or any other objective. This process of moving data from one place to another is known as database migration. Even though they are essential, data migration projects can be very complex.
A database migration tool allows firms to transfer data from one type of database to another, or from a database to another type of data repository such as a data warehouse or data lake, without having to rely on manual coding or overly complicated ETL tools.
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
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 DbMigrator
s 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With