user3471528
user3471528

Reputation: 3053

Start migrations in FluentMigrator

I need to use FluentMigrator in order to execute my database migrations. FluentMigrator seems to be a good and easy-to-use library. But I think I'm missing something... how to start a migration? How to set the database type? How to set the connection string?

In the GitHub I can't find a main() method or some entry point

Thanks a lot!

Upvotes: 2

Views: 2477

Answers (3)

Stas Ivanov
Stas Ivanov

Reputation: 1245

Most probably, you are looking for Migration runners. Here is the code for in-process migration runner from the documentation page:

using System;
using System.Linq;

using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization;

using Microsoft.Extensions.DependencyInjection;

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

            // Put the database update into a scope to ensure
            // that all resources will be disposed.
            using (var scope = serviceProvider.CreateScope())
            {
                UpdateDatabase(scope.ServiceProvider);
            }
        }

        /// <summary>
        /// Configure the dependency injection services
        /// </summary>
        private static IServiceProvider CreateServices()
        {
            return new ServiceCollection()
                // Add common FluentMigrator services
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    // Add SQLite support to FluentMigrator
                    .AddSQLite()
                    // Set the connection string
                    .WithGlobalConnectionString("Data Source=test.db")
                    // Define the assembly containing the migrations
                    .ScanIn(typeof(AddLogTable).Assembly).For.Migrations())
                // Enable logging to console in the FluentMigrator way
                .AddLogging(lb => lb.AddFluentMigratorConsole())
                // Build the service provider
                .BuildServiceProvider(false);
        }

        /// <summary>
        /// Update the database
        /// </summary>
        private static void UpdateDatabase(IServiceProvider serviceProvider)
        {
            // Instantiate the runner
            var runner = serviceProvider.GetRequiredService<IMigrationRunner>();

            // Execute the migrations
            runner.MigrateUp();
        }
    }
}

Upvotes: 1

UnitStack
UnitStack

Reputation: 1185

I wrote a helper for this, check it out here

https://github.com/Diginari/FluentMigrator-MVC-Helper

Upvotes: 1

Castrohenge
Castrohenge

Reputation: 8993

To run your migrations you need to use one of the Migration runners - https://github.com/schambers/fluentmigrator/wiki/Migration-Runners.

These allow you to run your migrations directly from the command line or from within Nant, MSBuild or Rake. The documentation outlines how to set your connection string and specify the database type.

Upvotes: 1

Related Questions