Fluent Migration:在 Process .net core 中 运行 Migrations 时如何指定配置文件
Fluent Migration: How to specify profile when running Migrations in Process .net core
在 fluent migrator 上,根据文档,这是一个关于如何在 .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();
}
}
}
如果要加载配置文件,如何指定配置文件?
我找到了。看最后一行。
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; })
这在文档中很难找到。发现它隐藏在 github 上的这个问题中。
在 fluent migrator 上,根据文档,这是一个关于如何在 .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();
}
}
}
如果要加载配置文件,如何指定配置文件?
我找到了。看最后一行。
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; })
这在文档中很难找到。发现它隐藏在 github 上的这个问题中。