.Net Core中AddDbContext配置CommandTimeout报错排查
解决EF Core中AddDbContext配置CommandTimeout的问题
你的代码错误在于给UseSqlServer传递了两个独立的委托参数,而UseSqlServer的第二个参数只接受一个**Action<SqlServerDbContextOptionsBuilder>类型的委托**,所有SQL Server相关配置都需要放在这个委托内部。
正确的写法是将UseRowNumberForPaging和CommandTimeout配置合并到同一个委托中:
services.AddDbContext<RewardsContext>(options => options.UseSqlServer( Configuration.GetConnectionString("MembershipContext"), sqlServerOptions => { sqlServerOptions.UseRowNumberForPaging(); sqlServerOptions.CommandTimeout(60); }), ServiceLifetime.Scoped);
如果偏好紧凑的链式写法,也可以这样写:
services.AddDbContext<RewardsContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MembershipContext"), sqlServerOptions => sqlServerOptions.UseRowNumberForPaging().CommandTimeout(60)), ServiceLifetime.Scoped);
这样修改后,两个配置项都会被正确应用,不会再出现参数不匹配或拼写相关的报错。
内容的提问来源于stack exchange,提问作者Chee Mun
相关产品推荐
相关产品推荐

