在FluentMigrator

时间:2015-08-29 11:52:15

标签: c# .net fluent-migrator migrator.net

我需要使用FluentMigrator才能执行数据库迁移。 FluentMigrator似乎是一个优秀且易于使用的库。 但我想我错过了什么......如何开始迁移?如何设置数据库类型?如何设置连接字符串?

在GitHub中我找不到main()方法或某些入口点

非常感谢!

3 个答案:

答案 0 :(得分:0)

要运行迁移,您需要使用其中一个迁移规则 - https://github.com/schambers/fluentmigrator/wiki/Migration-Runners

这些允许您直接从命令行或从Nant,MSBuild或Rake中运行迁移。该文档概述了如何设置连接字符串并指定数据库类型。

答案 1 :(得分:0)

我为此写了一个帮手,请在这里查看

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

答案 2 :(得分:0)

很可能您正在寻找 迁移跑步者 s 。这是进程内迁移运行程序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();
        }
    }
}