流利的迁移:在Process .net core中运行迁移时如何指定配置文件 [英] Fluent Migration: How to specify profile when running Migrations in Process .net core

查看:146
本文介绍了流利的迁移:在Process .net core中运行迁移时如何指定配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在流畅的迁移器上,根据文档,这是一个有关如何在.net核心上运行迁移的示例:

On fluent migrator and according to the documentation this is an example on how run migrations in process on .net core:

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
        /// </sumamry>
        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
        /// </sumamry>
        private static void UpdateDatabase(IServiceProvider serviceProvider)
        {
            // Instantiate the runner
            var runner = serviceProvider.GetRequiredService<IMigrationRunner>();

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

如果要加载一个配置文件,如何指定?

How do you specify a profile in case you want to load one?

推荐答案

我找到了它.看最后一行.

I found it. Look at the last line.

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())
            .Configure<RunnerOptions>(cfg => { cfg.Profile = profile; })

这在文档中很难找到.发现它被藏在gihub上.

This was wuite difficult to find in the documentation. Found it tucked away in this issue on gihub.

https://github.com/fluentmigrator/fluentmigrator/issues/886

这篇关于流利的迁移:在Process .net core中运行迁移时如何指定配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆