【问题标题】:DbContext.Database.SetCommandTimeout is not working as expected in ef core 3.1DbContext.Database.SetCommandTimeout 在 ef core 3.1 中没有按预期工作
【发布时间】:2020-12-30 04:08:45
【问题描述】:

我有简单的代码,我希望查询超时以测试我的异常日志记录,因此我使用 context.Database.SetCommandTimeout() 方法,提供 1 个刻度的最小值,但查询不会超时并从数据库。

我的 EF 版本是 EF Core 3.1.8。

代码如下所示:

await using var context = new CriusCommissionsDatabaseContext(_config);
context.Database.SetCommandTimeout(TimeSpan.FromTicks(1));
var saleSegments = await context.BSaleSegment 
                                .Include(x => x.Sale)
                                .Where(x => x.Sale.CorrelationId == correlationId)
                                .ToListAsync();

【问题讨论】:

    标签: c# .net-core entity-framework-core ef-core-3.1


    【解决方案1】:

    RelationalDatabaseFacadeExtensions 中运行SetCommandTimeout 函数时,函数中发生的情况是TimeSpan 转换为整数,TimeSpan.FromTicks(1) 将转换为0。根据DbCommand.CommandTimoout Property,它有以下备注

    实现者注意,建议 0 表示没有超时。

    所以在这种情况下,这意味着 TimeSpan.FromTicks(1) == 没有超时,因此您可以输入的最小值必须等于 int 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2018-05-03
      • 2020-11-14
      • 2020-04-25
      • 2017-07-17
      • 2020-10-06
      • 1970-01-01
      相关资源
      最近更新 更多