user1505521
user1505521

Reputation: 241

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.

My EF version is 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();

Upvotes: 0

Views: 1170

Answers (1)

ZarX
ZarX

Reputation: 1086

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. According to the DbCommand.CommandTimoout Property it has the following remarks

Note to implementers, it is recommended that 0 means no timeout.

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.

Upvotes: 2

Related Questions