我找到了答案!您在 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();
}
}
}