C# FluentMigrator.Runner 控制台应用程序设置每个会话的事务
C# FluentMigrator.Runner console app set transaction per session
我正在使用 FlunetMigrator.Runner.3.2.1,发现此版本不再默认回滚所有迁移,其中之一失败。它说它们是每个迁移文件!这不会增加价值。有没有办法在 运行 将其作为 .net 核心控制台应用程序时设置每个会话的事务。
我看到有这个 link https://fluentmigrator.github.io/articles/runners/runner-console.html 但是说使用我们没有使用的 Migrate.exe 文件,我们正在使用控制台。能否在代码中设置交易级别为session?
为什么有人想要 运行 它并且只完成一些更改,全有或全无是更好的方法
我找到答案了!您在 Runner 选项中设置它,如下所示
opt.TransactionPerSession = true;
创建 IServiceProvide 的完整方法
private static IServiceProvider CreateServices(string connectionString,
CommandLineArguments commandLineArguments)
{
return new ServiceCollection()
// Add common FluentMigrator services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
// Add SQL Server support to FluentMigrator
.AddSqlServer()
// Set the connection string
.WithGlobalConnectionString(connectionString)
// Define the assembly containing the migrations
.ScanIn(typeof(Program).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
.AddLogging(lb => lb.AddFluentMigratorConsole())
.Configure<RunnerOptions>(opt => {
opt.Tags = commandLineArguments.Tags.ToArray();
opt.TransactionPerSession = true; })
// Build the service provider
.BuildServiceProvider(false);
}
下面的代码是使用 IServiceProvider
的完整示例
var serviceProvider = CreateServices(connectionString, commandLineArguments);
// Put the database update into a scope to ensure
// that all resources will be disposed.
using (var scope = serviceProvider.CreateScope())
{
try
{
UpdateDatabase(scope.ServiceProvider, commandLineArguments);
}
catch (Exception e)
{
Console.WriteLine("There was a problem with the migration: " + e.Message + "\n" +
e.StackTrace);
}
migrationRun = true;
}
更新数据库代码:
private static void UpdateDatabase(IServiceProvider serviceProvider, CommandLineArguments commandLineArguments)
{
// Instantiate the runner
var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
if (commandLineArguments.Downgrade)
{
runner.MigrateDown(commandLineArguments.Version != -1 ? commandLineArguments.Version : 0);
}
else
{
if (commandLineArguments.Version != -1)
{
runner.MigrateUp(commandLineArguments.Version);
}
else
{
runner.MigrateUp();
}
}
}
我正在使用 FlunetMigrator.Runner.3.2.1,发现此版本不再默认回滚所有迁移,其中之一失败。它说它们是每个迁移文件!这不会增加价值。有没有办法在 运行 将其作为 .net 核心控制台应用程序时设置每个会话的事务。
我看到有这个 link https://fluentmigrator.github.io/articles/runners/runner-console.html 但是说使用我们没有使用的 Migrate.exe 文件,我们正在使用控制台。能否在代码中设置交易级别为session?
为什么有人想要 运行 它并且只完成一些更改,全有或全无是更好的方法
我找到答案了!您在 Runner 选项中设置它,如下所示
opt.TransactionPerSession = true;
创建 IServiceProvide 的完整方法
private static IServiceProvider CreateServices(string connectionString,
CommandLineArguments commandLineArguments)
{
return new ServiceCollection()
// Add common FluentMigrator services
.AddFluentMigratorCore()
.ConfigureRunner(rb => rb
// Add SQL Server support to FluentMigrator
.AddSqlServer()
// Set the connection string
.WithGlobalConnectionString(connectionString)
// Define the assembly containing the migrations
.ScanIn(typeof(Program).Assembly).For.Migrations().For.EmbeddedResources())
// Enable logging to console in the FluentMigrator way
.AddLogging(lb => lb.AddFluentMigratorConsole())
.Configure<RunnerOptions>(opt => {
opt.Tags = commandLineArguments.Tags.ToArray();
opt.TransactionPerSession = true; })
// Build the service provider
.BuildServiceProvider(false);
}
下面的代码是使用 IServiceProvider
的完整示例var serviceProvider = CreateServices(connectionString, commandLineArguments);
// Put the database update into a scope to ensure
// that all resources will be disposed.
using (var scope = serviceProvider.CreateScope())
{
try
{
UpdateDatabase(scope.ServiceProvider, commandLineArguments);
}
catch (Exception e)
{
Console.WriteLine("There was a problem with the migration: " + e.Message + "\n" +
e.StackTrace);
}
migrationRun = true;
}
更新数据库代码:
private static void UpdateDatabase(IServiceProvider serviceProvider, CommandLineArguments commandLineArguments)
{
// Instantiate the runner
var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
if (commandLineArguments.Downgrade)
{
runner.MigrateDown(commandLineArguments.Version != -1 ? commandLineArguments.Version : 0);
}
else
{
if (commandLineArguments.Version != -1)
{
runner.MigrateUp(commandLineArguments.Version);
}
else
{
runner.MigrateUp();
}
}
}