Fluent Migrator 3.3.2执行超时及RunnerContext过时问题求助
解决Fluent Migrator 3.3.2迁移超时及
RunnerContext过时问题 问题描述
使用Fluent Migrator 3.3.2执行数据库迁移时,抛出超时错误:
The error was Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
尝试通过已过时的runner.RunnerContext.Timeout = Timeout.Infinite;设置超时,触发空引用异常:
Migration exception: "Object reference not set to an instance of an object."
解决方案
在Fluent Migrator 3.x版本中,RunnerContext已被废弃,正确设置命令超时的方式是在构建迁移Runner时通过配置项指定,具体分两种场景:
1. 依赖注入场景(推荐)
如果使用.NET依赖注入框架配置Fluent Migrator,可在服务注册阶段通过WithCommandTimeout方法设置全局超时:
services.AddFluentMigratorCore() .ConfigureRunner(rb => rb .AddSqlServer() .WithGlobalConnectionString("YourConnectionString") .WithCommandTimeout(Timeout.Infinite) // 设置无限超时,也可指定具体毫秒数 .ScanIn(typeof(YourMigrationClass).Assembly).For.Migrations());
2. 手动创建Runner场景
若手动实例化迁移Runner,需通过RunnerOptions配置超时:
var runner = new MigrationRunner( new AssemblyMigrationSource(typeof(YourMigrationClass).Assembly), new RunnerOptions { CommandTimeout = Timeout.Infinite }, new SqlServerProcessor( new SqlServerConnection("YourConnectionString"), new NullAnnouncer(), new ProcessorOptions(), new SqlServerDbFactory())); runner.MigrateUp();
关键说明
CommandTimeout参数接受毫秒数,Timeout.Infinite表示无超时限制,也可根据实际需求设置具体数值(如300000即5分钟)。- 空引用异常的原因是3.3.2版本中
RunnerContext已不再被初始化,直接访问会导致null引用,因此必须使用上述替代方案。
内容的提问来源于stack exchange,提问作者Tropin Alexey
相关产品推荐
相关产品推荐

