Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IQueryable extention within db context #17276

Closed
ajcvickers opened this issue Aug 20, 2019 · 4 comments
Closed

IQueryable extention within db context #17276

ajcvickers opened this issue Aug 20, 2019 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Contributor

Moved from dotnet/core#3220 filed by @RomasB

Interface:

public interface IRemovable
{
    bool IsRemoved { get; set; }

    string RemovedByUser { get; set; }

    DateTime? Removed { get; set; }
}

Extention:

    public static List<T> List<T>(this IQueryable<T> query) where T : IRemovable
    {
        return query.Where(x => !x.IsRemoved).ToList();
    }

Usage:

  _dbContext.RemovableEntities.List()

Getting an exception like: "Operation is not valid due to the current state of the object."
Putting condition directly without extension everything works fine.

Looks like exception started to occur within .net core 3.0 preview 8

@RomasB
Copy link

RomasB commented Aug 20, 2019

Yes, EF Core issue.

Stack trace:

[Error] An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_01.b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetEnumerator()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at ???.DataModel.Infrastructure.Extentions.RemovableExtentions.List[T](IQueryable1 query) in C:\GitHub???Api???.DataModel.Infrastructure\Extentions\RemovableExtentions.cs:line 12
at ???.Essex.WebApi.Application.Controllers.MenuItemsController.GetMenuItems() in C:\GitHub???Api???.Essex.WebApi.Application\Controllers\MenuItemsController.cs:line 76
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.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
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()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
2019-08-20 14:30:29.823 +00:00 [Error] Exception caught

@ajcvickers
Copy link
Contributor Author

With daily builds:

Unhandled exception. System.InvalidOperationException: The LINQ expression '(b) => !(((IRemovable)b).IsRemoved)' could not be translated.
 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnu
merable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression sour
ce, LambdaExpression predicate)
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpr
ession)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression meth
odCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel m
odel, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Test.Program.List[T](IQueryable`1 query) in C:\Stuff\ThreeOhP7P\ThreeOhP7P\Program.cs:line 63
   at Test.Program.Main() in C:\Stuff\ThreeOhP7P\ThreeOhP7P\Program.cs:line 89

Process finished with exit code -532,462,766.

@ajcvickers
Copy link
Contributor Author

Note from triage: this was translated in 2.2:

dbug: Microsoft.EntityFrameworkCore.Query[10101]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Compiling query model:
      'from Blog x in DbSet<Blog>
      where !(((IRemovable)[x]).IsRemoved)
      select [x]'
dbug: Microsoft.EntityFrameworkCore.Query[10104]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Optimized query model:
      'from Blog x in DbSet<Blog>
      where !(((IRemovable)[x]).IsRemoved)
      select [x]'
dbug: Microsoft.EntityFrameworkCore.Query[10107]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      (QueryContext queryContext) => IEnumerable<Blog> _InterceptExceptions(
      |__ source: IEnumerable<Blog> _TrackEntities(
      |   |__ results: IEnumerable<Blog> _ShapedQuery(
      |   |   |__ queryContext: queryContext,
      |   |   |__ shaperCommandContext: SelectExpression:
      |   |   |       SELECT [x].[Id], [x].[Author], [x].[Content], [x].[IsRemoved], [x].[Removed], [x].[RemovedByUser], [x].[Title]
      |   |   |       FROM [Blogs] AS [x]
      |   |   |       WHERE [x].[IsRemoved] = 0,
      |   |   |__ shaper: (MaterializationContext materializationContext) =>
      |   |       {
      |   |           instance = new Blog()
      |   |           instance.<Id>k__BackingField = int TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 0, Blog.Id)
      |   |           instance.<Author>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 1, Blog.Author)
      |   |           instance.<Content>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 2, Blog.Content)
      |   |           instance.<IsRemoved>k__BackingField = bool TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 3, Blog.IsRemoved)
      |   |           instance.<Removed>k__BackingField = Nullable<DateTime> TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 4, Blog.Removed)
      |   |           instance.<RemovedByUser>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 5, Blog.RemovedByUser)
      |   |           instance.<Title>k__BackingField = string TryReadValue(ValueBuffer materializationContext.get_ValueBuffer(), 6, Blog.Title)
      |   |           return instance
      |   |       }),
      |   |__ queryContext: queryContext,
      |   |__ entityTrackingInfos: { itemType: Blog },
      |   |__ entityAccessors: List<Func<Blog, object>>
      |       {
      |           Func<Blog, Blog>,
      |       }),
      |__ contextType: Test.BloggingContext,
      |__ logger: DiagnosticsLogger<Query>,
      |__ queryContext: queryContext)
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'Test' on server '(localdb)\mssqllocaldb'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'Test' on server '(localdb)\mssqllocaldb'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [x].[Id], [x].[Author], [x].[Content], [x].[IsRemoved], [x].[Removed], [x].[RemovedByUser], [x].[Title]
      FROM [Blogs] AS [x]
      WHERE [x].[IsRemoved] = 0
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [x].[Id], [x].[Author], [x].[Content], [x].[IsRemoved], [x].[Removed], [x].[RemovedByUser], [x].[Title]
      FROM [Blogs] AS [x]
      WHERE [x].[IsRemoved] = 0
dbug: Microsoft.EntityFrameworkCore.ChangeTracking[10806]
      Context 'BloggingContext' started tracking 'Blog' entity. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see key values.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20300]
      A data reader was disposed.

@ajcvickers
Copy link
Contributor Author

@smitpatel Let's prepare a fix for this and take it to ask mode.

@ajcvickers ajcvickers added this to the 3.0.0 milestone Aug 26, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants