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.79k stars 3.19k forks source link

After upgrading to .Net 9 with EF 9 the global query filter code throws a runtime exception #35111

Open gorgasparyan opened 19 hours ago

gorgasparyan commented 19 hours ago

After upgrading to .Net 9 with EF 9 the global query filter code throws a runtime exception. Could be enum translation issue.

modelBuilder.Entity<TEntity>().HasQueryFilter(e => _dbSession.AoaSessionTypeEnum == AoaSessionTypeEnum.LandlordGroup ? _dbSession.AccountGroupAccountIds.Contains(e.AccountId) : e.AccountId == _dbSession.AccountId);

stack trace

System.InvalidOperationException: The LINQ expression 'DbSet<Property>()\n    .Where(p => (int)AoaDbContext._dbSession.AoaSessionTypeEnum == 4 ? __ef_filter__AccountGroupAccountIds_0\n        .Contains(p.AccountId) : (long?)p.AccountId == __ef_filter__AccountId_1)' 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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.\n   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)\n   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorExpression[TResult](Expression query)\n   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)\n   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass11_0`1.<ExecuteCore>b__0()\n   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)\n   at Aoa.Data.Infrastructure.Repository.Paging.PagedExtensions.ToPagedAsync[T](IQueryable`1 source, Int32 index, Int32 size, Int32 from, CancellationToken cancellationToken) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Data\\Infrastructure\\Repository\\Paging\\PagedExtensions.cs:line 17\n   at Aoa.Data.Infrastructure.Repository.Repository`1.GetListPagedAsync(Expression`1 predicate, Func`2 orderBy, Func`2 include, Int32 index, Int32 size, Boolean asNoTracking, Boolean asSplitQuery, Boolean ignoreQueryFilters, CancellationToken cancellationToken) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Data\\Infrastructure\\Repository\\Repository.cs:line 330\n   at Aoa.Data.Infrastructure.Repository.Repository`2.GetListPagedAsync(TIncludeSetting includeSetting, Expression`1 predicate, Func`2 orderBy, Int32 index, Int32 size, Boolean asNoTracking, Boolean asSplitQuery, Boolean ignoreQueryFilters, CancellationToken cancellationToken) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Data\\Infrastructure\\Repository\\Repository.cs:line 210\n   at Aoa.Core.Service.Property.PropertyService.GetPropertiesPagedAsync[TReturn](Nullable`1 multiUnitPropertyId, Nullable`1 verifiedOnly, PropertyIncludeSetting includeSetting, IPagedRequestModel pagedRequestModel, IFilteredRequestModel filteredRequestModel, IOrderedRequestModel orderedRequestModel) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Core\\Service\\Property\\PropertyService.cs:line 67\n   at Aoa.Api.Controllers.Landlord.Property.PropertiesController.GetPropertiesPagedAsync(Nullable`1 multiUnitPropertyId, PropertyIncludeSetting includeSetting, PagedRequestModel pagedRequestModel, FilteredRequestModel filteredRequestModel, OrderedRequestModel orderedRequestModel) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Api\\Controllers\\Landlord\\Property\\PropertiesController.cs:line 45\n   at lambda_method2870(Closure, Object)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\n--- End of stack trace from previous location ---\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\n--- End of stack trace from previous location ---\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger)\n   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)\n   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\n   at Aoa.Api.Infrastructure.Middleware.AoaPermissionMiddleware.InvokeAsync(HttpContext httpContext, IMapper mapper, IAoaSession session, IAoaDbSession dbSession, IPermissionService permissionService) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Api\\Infrastructure\\Middleware\\AoaPermissionMiddleware.cs:line 58\n   at Aoa.Api.Infrastructure.Middleware.AoaSessionMiddleware.InvokeAsync(HttpContext httpContext, IAoaSession session, IAoaDbSession dbSession, IIdentityUserService identityUserService) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Api\\Infrastructure\\Middleware\\AoaSessionMiddleware.cs:line 48\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\n   at DevExpress.AspNetCore.Internal.ResourceManagerMiddleware.Invoke(HttpContext httpContext)\n   at Aoa.Api.Common.Infrastructure.Middleware.AoaExceptionMiddleware.InvokeAsync(HttpContext httpContext) in C:\\Documents\\Projects\\aoateam\\aoa-portal\\aoa-portal-api\\Aoa.Api.Common\\Infrastructure\\Middleware\\AoaExceptionMiddleware.cs:line 49

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 9.0) Operating system: IDE: (e.g. Visual Studio 2022 17.12.0)

roji commented 18 hours ago

@gorgasparyan can you please post a minimal, runnable code sample? It's usually very difficult to pinpoint the problem from a single short snippet such as the above.