EF Core 2.0 CountAsync Cause Timeout SqlException
Exception message:
[Error] Failed executing DbCommand ("2"ms) [Parameters=["@__Uid_0='?'"], CommandType='Text', CommandTimeout='30']"
""SELECT COUNT(*)
FROM [Question_Answer] AS [q]
INNER JOIN [question_Item] AS [q.questionItem] ON [q].[QID] = [q.questionItem].[QID]
INNER JOIN [Club_Users] AS [q.userinfo] ON [q].[UserID] = [q.userinfo].[UserID]
WHERE ((([q].[IsActive] = 1) AND ([q.userinfo].[IsActive] = 1)) AND (([q].[UserID] = @__Uid_0) AND ([q.questionItem].[IsActive] = 1))) AND ([q.userinfo].[IsActive] = 1)"
System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): Unknown error 258
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
ClientConnectionId:97872af3-7c2e-438f-b1c4-76769b91870f
Error Number:-2,State:0,Class:11
Stack trace:
An exception occurred in the database while iterating the results of a query for context type '"Robert.Data.Question.EfUnitOfWork"'."
""System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): Unknown error 258
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__108_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<BufferlessMoveNext>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.AsyncQueryMethodProvider.<GetResult>d__16`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
ClientConnectionId:97872af3-7c2e-438f-b1c4-76769b91870f
Error Number:-2,State:0,Class:11"
Steps to reproduce
return await _answers.CountAsync(spec.SatisfiedBy());
Further technical details
EF Core version: 2.0 Database Provider: sql 2008 r2 Operating system: ubuntu 16.04 IDE: ( Visual Studio2017)
@doublnt - There is not enough information on how to reproduce this issue. You have return the line which is throwing but we don't know anything about what is the value of spec and info on method SatisfiedBy() which hides exactly what query is being run. Please provide a complete repro which we can run or provide the linq query which is being passed to EF to run without using any other framework/methods.
The return line is something like that
return await _answers.CountAsync(a => a.UserId == id && a.questionItem.IsActive && a.userinfo.IsActive && a.ISActive);
@doublnt Unfortunately, this is still not enough information for us to be able to reproduce what is going on. Could you provide a full code listing or project that exhibits what you are seeing?
EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.
BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.
I know exactly what you @doublnt are talking about. Using a CountAsync I get thousands of the select * I will do my best to try an recreate steps. And post a workaround to the countAsync() flaw.
With my situation, I am testing withSQL using a Select * which I know is bad. I am testing. But the main point is the iquerable CountAsync is doing a SELECT COUNT(*) FROM () AS [p]) over and over again for every record that is in the dataset which I cannot see the point in that, probably a flaw in countAsync()
When I specify specific columns, the speed of doing the unnecessary queries when using WithSQL will be lessened, but still a flaw. I dont have a good workaround yet.

public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source, int pageIndex, int pageSize)
{
var count = await source.CountAsync();
var items = await source.Skip(
(pageIndex - 1) * pageSize)
.Take(pageSize).ToListAsync();
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
I get thousands of these in SQL profilier
SELECT COUNT(*)
FROM (
SELECT * FROM dbo.fnt_Vendor_SearchBoard()
) AS [p]
WHERE (CHARINDEX(@__searchName_1, [p].[VendorIdentity]) > 0) OR (@__searchName_1 = N'')
Then a SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.