简体   繁体   English

DbContext.Database.SetCommandTimeout 在 ef 核心 3.1 中未按预期工作

[英]DbContext.Database.SetCommandTimeout is not working as expected in ef core 3.1

I have simple code where I want the query to timeout to test my exception logging, hence I am using the context.Database.SetCommandTimeout() method, supplying the minimal value of 1 tick, but the query does not time out and fetches the data from the database.我有简单的代码,我希望查询超时以测试我的异常日志记录,因此我使用context.Database.SetCommandTimeout()方法,提供 1 个滴答的最小值,但查询不会超时并获取数据从数据库。

My EF version is EF Core 3.1.8.我的 EF 版本是 EF Core 3.1.8。

The code looks like this:代码如下所示:

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();

When running the SetCommandTimeout function in the RelationalDatabaseFacadeExtensions what happens in in the function is that the TimeSpan is converted to an integer and TimeSpan.FromTicks(1) will be converted to 0 .RelationalDatabaseFacadeExtensions 中运行SetCommandTimeout函数时,函数中发生的情况是 TimeSpan 转换为整数,而TimeSpan.FromTicks(1)将转换为0 According to the DbCommand.CommandTimoout Property it has the following remarks根据DbCommand.CommandTimoout 属性,它有以下注释

Note to implementers, it is recommended that 0 means no timeout.请注意实现者,建议 0 表示没有超时。

So in this case it means TimeSpan.FromTicks(1) == no timeout, so the minimal value you would be able to enter would have to be equal to int 1.所以在这种情况下,它意味着 TimeSpan.FromTicks(1) == 没有超时,所以你可以输入的最小值必须等于 int 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM