1

我需要使用 FluentMigrator 来执行我的数据库迁移。FluentMigrator 似乎是一个很好且易于使用的库。但我想我错过了一些东西......如何开始迁移?如何设置数据库类型?如何设置连接字符串?

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

非常感谢!

4

3 回答 3

0

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

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

于 2015-11-02T16:53:59.893 回答
0

要运行迁移,您需要使用迁移运行器之一 - https://github.com/schambers/fluentmigrator/wiki/Migration-Runners

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

于 2015-09-22T11:03:41.693 回答
0

很可能,您正在寻找Migration runner以下是文档页面中进程内迁移运行器的代码:

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();
        }
    }
}
于 2019-06-05T08:14:40.753 回答