SRoddis/Mongo.Migration

How to know the migration version of the database?

Opened this issue · 1 comments

Hi, I want to get the version of the database in a status controller in netcore. How can I find out in what version is the database?

have you tried IOC-ing the IDatabaseVersionService? It's a public interface, and it implements the GetLatestDatabaseVersion method which the project itself uses to determine the version. Add the following two parameters to your controller constructor:

public MyControllerConstructor(IMongoDatabase db, IDatabaseVersionService versionService, ... other constructor parameters here)

Then your controller method to get the version would look something like this:

    public IActionResult GetVersion()
    {
        DocumentVersion dbVersion = versionService.GetLatestDatabaseVersion(db);
        // now do something with it
    }

The IDatabaseVersionService gets registered when calling AddMigrations, and if you follow the .net Core setup as per the readme, you also have the IMongoDatabase so you should be good to go.