【问题标题】:Start migrations in FluentMigrator在 FluentMigrator 中开始迁移
【发布时间】:2015-11-23 23:34:57
【问题描述】:

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

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

非常感谢!

【问题讨论】:

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


    【解决方案1】:

    很可能,您正在寻找 Migration runners。这是进程内迁移运行器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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-19
          • 1970-01-01
          • 2016-09-23
          • 2011-09-06
          • 2023-04-10
          • 1970-01-01
          相关资源
          最近更新 更多