How to generate Fluent Migrations and Update/Rollback from Visual Studio?

Discover how easily generate, update and rollback fluent migration in Visual Studio.

Problem

FluentMigrator is a great tool when you need to manage database changes. My recommendation is to check it if you didn’t try yet. But if you came from the Entity Framework world you will quickly find a downside.

EF Migrations allows you easily generate migrations by a command in the Power Manager Console. You can generate a new migration, update, or rollback database. FluentMigrator at the same time does not have a migration generator and you should remember commands to apply/rollback migrations or you probably have a .bat file that does it for you.

So, what FluentMigrator is missing:

  • migration generation with a timestamp key
  • easy way to apply a migration
  • easy way to rollback migration

IMHO it is a big disadvantage compared to EF Migrations because you need to spend more time and learn FluentMigrator commands and parameters.

Solution - Alt.FluentMigrator.VStudio

Alt.FluentMigrator.VStudio is a simple set of commands that solves all the listed problems above and even does more comparing to EF Migrations. It is developed as a NuGet package you can find here and also you can find quick documentation on GitHub: https://github.com/crimcol/Alt.FluentMigrator.VStudio.

1 Installation and configuration

Let’s say you develop a web application with .NET 6. and you have as a minimum two projects:

  • WebApplication - ASP.NET Core app which contains a connection string;
  • DbMigrations - class library responsible for database migrations.

Solution Structure

1.1 Install FluentMigrator and FluentMigrator.Console packages

1
2
Install-Package FluentMigrator
Install-Package FluentMigrator.Console

Packages installation

1.2 Install Alt.FluentMigrator.VStudio and a create configuration file

1
Install-Package Alt.FluentMigrator.VStudio

Once the package has been installed you can create a default migrations.json configuration and set do not copy to output directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
    "ConnectionProjectName": "WebApplication",
    "ConnectionName": "TestDb",
    "FluentMigrationToolPath": "%USERPROFILE%\\.nuget\\packages\\fluentmigrator.console\\3.3.2\\net461\\any\\Migrate.exe",
    "DbProvider": "SqlServer",
    "DbProviderHelpUrl": "https://fluentmigrator.github.io/articles/runners/runner-console.html#--provider---dbtype---dbvalue-required",
    "MigrationFolder": "Migrations",
    "ScriptsFolder":  "Scripts",
    "TimeFormat": "yyyyMMddHHmmss"
}

Let’s add a connection string to the WebApplication project if you don’t have it yet:

1
2
3
"ConnectionStrings": {
    "TestDb": "server=localhost\\SQLEXPRESS; database=TestDb; Integrated Security=SSPI"
    }

What we can change in the migrations.json file

  • ConnectionProjectName - the name of the project contains Web.config or App.config file with a connection string.
  • ConnectionName - connection name. TestDb in our case.
  • FluentMigrationToolPath - relative path to the console runner. Depends on what version of FluentMigrator.Console package you have might be located in another folder.
  • DbProvider - database provider. You can change provider here if you use MySql or PostgreSQL, etc. List of all providers here.
  • MigrationFolder - folder name in your project where all migrations will be created. it also supports subfolders. ‘Migrations’ by default.
  • ScriptsFolder - folder name for SQL scripts.
  • TimeFormat - a time format is a number that will be used as a migration version and part of the file name. The time format “yyMMddHHmm” is also applicable and contains fewer characters.
Tip
My recommendation is to use a time format with seconds "yyyyMMddHHmmss". In this case, you can avoid situations when another developer may generate a migration with the same version.

2 Create a migration

To create a new migration you just need to run the command in the Package Manager Console:

1
Add-FluentMigration InitialMigration

Add initial migration

20221128002948_InitialMigration.cs file will look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using FluentMigrator;

namespace DbMigrations
{
  [Migration(20221128002948)]
  public class InitialMigration : Migration
  {
    public override void Up()
    {
      
    }

    public override void Down()
    {
      
    }
  }
}
Note
It will create a class with a Migration attribute and a timestamp number as a version. The file name also has the same timestamp number, so all your migrations will be in the correct order.

3 Create a migration with a script

For those who would like to write SQL script instead of writing C# code, you can generate a migration with a script.

1
Add-FluentMigration SqlScriptMigration -AddScript

As you can see this command will generate Up and Down SQL scripts and set the file path in fluent migration .cs file which I found extremely convenient.

Add second migration

4 Update database

Updating the database is the same simple as generating a new migration, just run the command Update-FluentDatabase and it will apply all migrations which don’t exist in the database.

Update database

5 Rollback migration

1
2
Rollback-FluentDatabase 0                  # Rollback all migrations
Rollback-FluentDatabase 20191207220215     # Rollback to the given version

Rollback database

Note
When you run the Update or Rollback command it will automatically build a project to avoid future bugs.

What .Net Framework can be used?

In the GitHub repository you may find an Example that contains the following projects:

  • Console app with .NET 6
  • Web app with .NET 6
  • Class Library with .NET 3.1
  • Class Library with .NET Framework 4.8

I personally use it on a legacy project with .NET Framework 4.8 and on a new project with .NET 6.

Conclusion

Alt.FluentMigrator.VStudio NuGet package is a great helper to everyday work with database changes. If your application depends on a database and you often need to do database changes this small package is a huge time saver and for a whole team. You can simply generate a new migration, update, or rollback similar to Entity Framework.

I hope you enjoyed the demonstration and learned something new.


 P.S. This package was developed by me. You can give a star to the GitHub repository and share it with your colleagues.


Resources

0%