Postgresql 在数组类型上使用EF.Functions.Like时EF Core抛出异常

Postgresql 在数组类型上使用EF.Functions.Like时EF Core抛出异常,postgresql,asp.net-core,entity-framework-core,Postgresql,Asp.net Core,Entity Framework Core,通过遵循中的文档,我应该能够在数组中搜索字符串,但事实并非如此。这里是我的代码片段 getFiles=await\u context.Files.Include(r=>r.Box) .Where(r=>r.Box.AccountId==AccountId&&r.SealCodes.Any(s=>EF.Functions.Like(searchFile,s))) .OrderByDescending(c=>c.CreatedOn) .ToListAsync(); 这里是异常消息,我不太清楚发生了

通过遵循中的文档,我应该能够在数组中搜索字符串,但事实并非如此。这里是我的代码片段

getFiles=await\u context.Files.Include(r=>r.Box)
.Where(r=>r.Box.AccountId==AccountId&&r.SealCodes.Any(s=>EF.Functions.Like(searchFile,s)))
.OrderByDescending(c=>c.CreatedOn)
.ToListAsync();
这里是异常消息,我不太清楚发生了什么

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware
An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The LINQ expression 'DbSet<FileBox>
    .Where(f => f.SealCodes
        .Any(s => __Functions_0
            .Like(
                _: __searchFile_1,
                matchExpression: s)))' 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 AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
   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.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_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 Subalipack.Fms.V2.WebAdmin.Controllers.FileController.LoadTable(FileParametersDto dtParameters, String From, String To) in C:\Users\user\Documents\Project\Subalipack\FMS\Source Code\Subalipack.Fms.V2.WebAdmin\Controllers\FileController.cs:line 101
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>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>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>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>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware
执行请求时发生未经处理的异常。
System.InvalidOperationException:LINQ表达式“DbSet”
.其中(f=>f.SealCodes
.Any(s=>\u\u函数\u 0
.像(
_:\u搜索文件\u 1,
无法翻译匹配表达式(s))。以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算。看见https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。
在Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslationExpressionVisitor.g_uCheckTranslated|8_0(ShapedQueryExpression translated,c_uDisplayClass8_0&)
在Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslationExpressionVisitor.VisitMethodCall调用(MethodCallExpression MethodCallExpression)
位于Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslationExpressionVisitor.VisitMethodCallExpression(MethodCallExpression MethodCallExpression)
位于System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor)
在Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslationExpressionVisitor.VisitMethodCall调用(MethodCallExpression MethodCallExpression)
位于Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslationExpressionVisitor.VisitMethodCallExpression(MethodCallExpression MethodCallExpression)
位于System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor)
位于Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](表达式查询)
位于Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](表达式查询,布尔异步)
位于Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase数据库、表达式查询、IModel模型、布尔异步)
在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.c__DisplayClass9_0`1.b__0()中
位于Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](对象缓存键,Func`1编译器)
位于Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOradQuery[TResult](对象缓存键,Func`1编译器)
在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](表达式查询)中
在Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](表达式)中
在Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()中
位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
在C:\Users\user\Documents\Project\Subalipack\Fms\Source Code\Subalipack.Fms.V2.WebAdmin.Controllers.FileController.LoadTable(fileParametersDTParameters,String From,String To)中的Subalipack.Fms.V2.WebAdmin\Controllers\FileController.cs:第101行
位于Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper映射器、ObjectMethodExecutor执行器、对象控制器、对象[]参数)
在Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g|u等待| 12_0(ControllerActionInvoker invoker,ValueTask`1 actionResultValueTask)
在Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g_u等待| 10_0(ControllerActionInvoker invoker,Task lastTask,State next,Scope Scope,Object State,Boolean isCompleted)
位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed上下文)
位于Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(状态和下一步、范围和范围、对象和状态、布尔值和isCompleted)
在Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g_u等待| 13_0(ControllerActionInvoker invoker,Task lastTask,State next,Scope Scope,Object State,Boolean isCompleted)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_|24_0(ResourceInvoker invoker、Task lastTask、State next、Scope Scope、Object State、Boolean isCompleted)
位于Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed上下文)
位于Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(状态和下一步、范围和范围、对象和状态、布尔值和isCompleted)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_|19_0(ResourceInvoker invoker、Task lastTask、State next、Scope Scope、Object State、Boolean isCompleted)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g|u waiting | 17|0(ResourceInvoker invoker,Task Task,IDisposable作用域)
位于Microsoft.AspNetCore.Routing.EndpointMiddleware.g_uwaitRequestTask | 6_0(端点、任务请求任务、ILogger记录器)
位于Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext上下文)
使用asp.net core 3.1和
Npgsql.EntityFrameworkCore.PostgreSQL
3.1.4版


有什么想法吗?

我只是在数据模型上从
List
更改为
string[]
,然后问题就解决了。

我只是从
List
更改为
string[]
在数据模型上,然后问题就消失了。

发生此异常是因为在.NetCore 3.1中,您不能调用无法转换为SQL的LINQ,除非您在此之前调用了ToList()
getFiles = await _context.Files.Include(r => r.Box)
                        .ToListAsync()
                        .Where(r => r.Box.AccountId == accountId && r.SealCodes.Any(s => EF.Functions.Like(searchFile, s)))
                        .OrderByDescending(c => c.CreatedOn)
                        .ToListAsync();