When the result of max is 0 a error message is generated
``` C#
_ctx.ChapterQuestions.Where(x => x.SurveyChapterId == surveyChapterId).Max(x => x.SequenceNumber+0) // produce error
_ctx.ChapterQuestions.Where(x => x.SurveyChapterId == surveyChapterId).Max(x => x.SequenceNumber+1) // is allowed
sql data
Id SequenceNumber
2293 0
stack trace
dbug: Microsoft.EntityFrameworkCore.Query[10107]
(queryContext) => Single
(RelationalQueryContext)queryContext,
SqlServerQuerySqlGeneratorFactory,
SqlExpressionFactory,
ParameterNameGeneratorFactory,
Projection Mapping:
EmptyProjectionMember -> 0
SELECT MAX(c.SequenceNumber + 0)
FROM ChapterQuestions AS c
WHERE (c.SurveyChapterId == @__surveyChapterId_0) && NotEqual(@__surveyChapterId_0),
Func
Survey.Entities.Context.SurveyContext,
DiagnosticsLogger
))
dbug: Microsoft.EntityFrameworkCore.Database.Command[20103]
Creating DbCommand for 'ExecuteReader'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20104]
Created DbCommand for 'ExecuteReader' (0ms).
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
Opening connection to database 'survey' on server '(localdb)\mssqllocaldb'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
Opened connection to database 'survey' on server '(localdb)\mssqllocaldb'.
info: Microsoft.EntityFrameworkCore.Database.Command[20100]
Executing DbCommand [Parameters=[@__surveyChapterId_0='702'], CommandType='Text', CommandTimeout='30']
SELECT MAX([c].[SequenceNumber] + 0)
FROM [ChapterQuestions] AS [c]
WHERE ([c].[SurveyChapterId] = @__surveyChapterId_0) AND @__surveyChapterId_0 IS NOT NULL
dbug: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (3ms) [Parameters=[@__surveyChapterId_0='702'], CommandType='Text', CommandTimeout='30']
SELECT MAX([c].[SequenceNumber] + 0)
FROM [ChapterQuestions] AS [c]
WHERE ([c].[SurveyChapterId] = @__surveyChapterId_0) AND @__surveyChapterId_0 IS NOT NULL
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'Survey.Entities.Context.SurveyContext'.
System.InvalidOperationException: Sequence contains no elements.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.QueryingEnumerable1.Enumerator.MoveNext()
System.InvalidOperationException: Sequence contains no elements.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.QueryingEnumerable1.Enumerator.MoveNext()
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
A data reader was disposed.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20002]
Closing connection to database 'survey' on server '(localdb)\mssqllocaldb'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20003]
Closed connection to database 'survey' on server '(localdb)\mssqllocaldb'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
Executed action Survey.WebApi.Survey.ManageSurveys.Controllers.SurveyManagementController.InsertChapterQuestion (Survey.WebApi) in 39.4262ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
Executed endpoint 'Survey.WebApi.Survey.ManageSurveys.Controllers.SurveyManagementController.InsertChapterQuestion (Survey.WebApi)'
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Sequence contains no elements.
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.QueryingEnumerable1.Enumerator.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteTResult
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteTResult
at System.Linq.Queryable.MaxTSource,TResult
at Survey.WebApi.Survey.ManageSurveys.DbServices.ManageSurveyQuestionProvider.InsertChapterQuestion(Int32 surveyId, Int32 surveyChapterId, Int32 questionId) in C:\git\mijnzorgpeiler\Survey\Survey.WebApi\Survey\ManageSurveys\DbServices\ManageSurveyQuestionProvider.cs:line 50
at Survey.WebApi.Survey.ManageSurveys.Services.ManageSurveyService.InsertChapterQuestion(Int32 surveyId, Int32 surveyChapterId, Int32 questionId) in C:\git\mijnzorgpeiler\Survey\Survey.WebApi\Survey\ManageSurveys\Services\ManageSurveyService.cs:line 69
at Survey.WebApi.Survey.ManageSurveys.Controllers.SurveyManagementController.InsertChapterQuestion(Int32 surveyId, Int32 surveyChapterId, Int32 questionId) in C:\git\mijnzorgpeiler\Survey\Survey.WebApi\Survey\ManageSurveys\Controllers\SurveyManagementController.cs:line 59
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
```
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.1)
Operating system:
IDE: (e.g. Visual Studio 2019 16.4)
May I ask when version 3.1.1 can be released, I also encountered the same problem.
I found the weekly status updates link
Most helpful comment
May I ask when version 3.1.1 can be released, I also encountered the same problem.
I found the weekly status updates link