dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.68k stars 3.17k forks source link

InvalidOperationException: Processing of the LINQ expression after upgrade to .Net Core 3.1 (from 2.2) #19510

Open scalablecory opened 4 years ago

scalablecory commented 4 years ago

From @ferronsw on Tuesday, January 7, 2020 2:14:03 PM

I'm running Visual Studio 2019 on Windows 10 1809. After I've upgraded my app from .Net Core 2.2 to 3.1 I'm getting an Linq error with this code:

var userGroupGuids = ClaimsPrincipalExtension.GetUserGroupGuids(principal);
var avaiablePortal = customerPortals.Select(x => x.Group).Intersect(userGroupGuids).ToList();

This worked just fine before the migration. When I change the second line to this is works:

var avaiablePortal = customerPortals.ToList().Select(x => x.Group).Intersect(userGroupGuids).ToList();

Is this because of a change or a bug?

Trace:

InvalidOperationException: Processing of the LINQ expression 'DbSet<Portal> .Where(x => x.CustomerId == __customer_Id_0) .Select(x => x.Group) .Intersect(__p_1)' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.

Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
    System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
    Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
    Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
    Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor<TResult>(Expression query)
    Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery<TResult>(Expression query, bool async)
    Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore<TResult>(IDatabase database, Expression query, IModel model, bool async)
    Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass9_0<TResult>.<Execute>b__0()
    Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<TFunc>(object cacheKey, Func<Func<QueryContext, TFunc>> compiler)
    Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<TResult>(object cacheKey, Func<Func<QueryContext, TResult>> compiler)
    Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<TResult>(Expression query)
    Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<TResult>(Expression expression)
    Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<TResult>.GetEnumerator()
    System.Collections.Generic.List<T>..ctor(IEnumerable<T> collection)
    System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
    Switch.OLW.Client.Helpers.Portal.LayoutBuilder.Build(ClaimsPrincipal principal) in LayoutBuilder.cs

                    var avaiablePortal = customerPortals.Select(x => x.Group).Intersect(userGroupGuids).ToList();

Switch.OLW.Client.Controllers.HomeController.Index() in HomeController.cs

                Portal portal = _layoutBuilder.Build(User);

lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Copied from original issue: dotnet/core#4087

smitpatel commented 4 years ago

By design. We don't translate Intersect with client side List to server. Exception message improved to throw client evaluation error in 5.0

ajcvickers commented 4 years ago

@smitpatel Close as by-design?

ptsneves commented 4 years ago

Can we get the why this is by design and how to achieve an intersection between a user set and a server? This is a perfectly legitimate operation and just saying by design is not very community friendly.

ajcvickers commented 4 years ago

@smitpatel Is "Intersect with client side List" something we have on the backlog?

smitpatel commented 4 years ago

We probably don't have any thing in the backlog. In order to process this on server side, we need to send whole list to server side in some form. With that list of unmapped scalars is out of question. There are various different themes for the rest.