You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:55:45