0

Is there a way for code in my ASP.NET MVC 4, code-first EF app to retrieve the current migration name? I want to display the migration name on an administrator's status page just as a sanity check to verify that the expected migration(s) have been applied.

Bob.at.Indigo.Health
  • 11,023
  • 13
  • 64
  • 111

1 Answers1

1

You can use the DbMigrator (DbMigrator) class for that.

e.g.

var migrator = new DbMigrator(_configuration);
var pending = migrator.GetPendingMigrations();
var all = migrator.GetLocalMigrations();

Where _configuration is your Configuration class under the Migraiton dir.

You need to experiment a bit - see which actually fits your bill.

Also, I'm suggesting that you make an 'initializer' instead of just adding that into the code. As that's how it's usually done, and a 'natural spot' for those things to happen (you don't 'call it', it 'calls you').

Check this link for an implementation of a custom initializer - which includes some DbMigrator code.
How to create initializer to create and migrate mysql database?

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • It works perfect but it takes more than 30 seconds to response. any suggestions please?? – Farshad Nasehi Dec 06 '22 at 12:59
  • 1
    It's probably your setup (too many migrations or Db etc.), is all I can think of. Generally, this isn't something you should do on normal app runs, when performance is an issue, but on occasional maintenance or setup or something. – NSGaga-mostly-inactive Dec 06 '22 at 14:46