· 1 min read

AutoReversing Migrations in FluentMigrator

Know how to automatically generate reverse migrations in FluentMigrator using the AutoReversingMigration class.

Know how to automatically generate reverse migrations in FluentMigrator using the AutoReversingMigration class.

One interesting thing I found today in FluentMigrator while typing through IntelliSense is the AutoReversingMigration class. This class is used to generate the reverse migration automatically. This is slightly different from the regular Migration class.

The AutoReversingMigration class doesn’t have a Down() method. Instead, it automatically generates the reverse migration based on the Up() method.

Here is an example of how to use the AutoReversingMigration class.

using FluentMigrator;

[Migration(20241101100000)]
public class AddNewTable : AutoReversingMigration
{
    public override void Up()
    {
        Create.Table("NewTable")
            .WithColumn("Id").AsInt32().PrimaryKey().Identity()
            .WithColumn("Name").AsString(50).NotNullable();
    }
}

However, the AutoReversingMigration class has some limitations compared to the regular Migration class. For example, you can’t figure out the reverse migration for Execute.EmbeddedScript() or Execute.Script(), and you can’t use the AutoReversingMigration class for those scenarios.

According to the FluentMigrator documentation, the only methods that are supported by the AutoReversingMigration class are:

  • Create.Table()
  • Create.Column()
  • Create.ForeignKey()
  • Create.Index()
  • Create.Schema()
  • Rename.Table()
  • Rename.Column()
  • Delete.ForeignKey()

For other scenarios, you still have to use the regular Migration class and implement the Down() method manually.

Back to Blog

Related Posts

View All Posts »
Using Dapper with FluentMigrator

Using Dapper with FluentMigrator

In this post, we will explore how to use Dapper with FluentMigrator for schema migrations in ASP.NET Core. We will set up a simple project, and use FluentMigrator to manage the schema migrations in code and run them during the application startup.

Writing Tests for Dapper with TestContainers in xUnit

Writing Tests for Dapper with TestContainers in xUnit

Writing tests for Dapper is traditionally challenging due to two reasons. In this post, we will explore how to write integration test fixtures for Dapper queries using TestContainers in xUnit and how to reuse the database container across multiple tests.