DEV Community

Mirnes
Mirnes

Posted on • Originally published at optimalcoder.net on

2

Keep Your Database In Sync With Application: Fluent Migrator

In this post, we continue to explore ways to free your application from being tightly bound to the Entity Framework environment. The primary goal is to offer an alternative solution that is not only more flexible and robust but also gives you, as a developer, greater control over database manipulation with less abstraction and complexity. Here, we introduce a library for database migrations, which serves as a natural extension to tools like Dapper and SqlKata.

Fluent Migrator

Fluent Migrator is a library designed for managing database migrations, giving you complete control over the execution and how migrations are written. It offers greater flexibility compared to standard EF migrations, which are closely tied to EF entities and generated automatically, often accompanied by additional overhead, including extra snapshot files.

In the code snippet below, you’ll find a simple migration class. This class includes two methods that override the base Migration class, allowing you to define your migration. This is all you need to create a migration.

using FluentMigrator;

[Migration(2025011801)]
public class Mig2025011801Init : Migration
{
    public override void Up()
    {
        Create.Table("Company")
            .WithColumn("Id").AsInt32().PrimaryKey().Identity()
            .WithColumn("Name").AsString(255).NotNullable()
    }

    public override void Down()
    {
        Delete.Table("Company");
    }
}



Enter fullscreen mode Exit fullscreen mode

Additionally, when starting the project, we need to configure FluentMigrator to locate the migration classes and define how they should be executed. This is demonstrated in the code snippet below.

using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;

class Program
{
    static void Main(string[] args)
    {
        var serviceProvider = CreateServices();

        using (var scope = serviceProvider.CreateScope())
        {
            UpdateDatabase(scope.ServiceProvider);
        }
    }

    private static IServiceProvider CreateServices()
    {
        return new ServiceCollection()
            .AddFluentMigratorCore()
            .ConfigureRunner(rb => rb
                .AddSqlServer()
                .WithGlobalConnectionString("conn string")
                .ScanIn(typeof(Mig2025011801Init).Assembly).For.Migrations())
            .AddLogging(lb => lb.AddFluentMigratorConsole())
            .BuildServiceProvider(false);
    }

    private static void UpdateDatabase(IServiceProvider serviceProvider)
    {
        var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
        runner.MigrateUp();
    }
}



Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion, let us show one table where we see simple comparison between FluentMigrator and EF migrations. This can help you to decide which migration library to use for your next or existing project.

Fluent Migrator EF Migrations
Schema migrations for databases with full control over structure Built-in to EF for schema migrations tied to EF models
Independent of ORM (works with raw ADO.NET, Dapper, or others) Closely integrated with Entity Framework
Requires writing migrations manually, which can be verbose but provides full control. Migrations are auto-generated and customizable in C#.
Allows full control, suitable for existing schemas Focused on Code First or Model First approaches
Explicitly define Down() methods for each migration. Provides precise control over rollback logic Rollbacks involve reverting migrations using commands like Update-Database -TargetMigration
Integrates well into CI/CD pipelines as a standalone migration tool. Can be used independently of application deployment Integrated into EF workflows, making deployment tied to EF-related commands. Often relies on the application's context to execute migrations

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay