c# - 无法翻译 LINQ 表达式 DbSet<>.Any
问题描述
我正在使用 ConsmoDB 和 Entity Framework Core 3.1。需要检查实体是否存在
bool callExists = await _context.Calls
.AsNoTracking()
.AnyAsync(x => x.Number == request.Number && x.CustomerId == request.CustomerId, cancellationToken);
if (callExists)
{
throw new ConflictException($"Call already exists");
}
运行后,出现以下异常
System.InvalidOperationException:无法翻译 LINQ 表达式“DbSet .Any(c => c.Number == __request_Number_0 && c.CustomerId == __request_CustomerId_1)”。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。请参阅 https://go.microsoft.com/fwlink/?linkid=2101038了解更多信息。在 Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.g__CheckTranslated|8_0(ShapedQueryExpression 已翻译,<>c__DisplayClass8_0& ) 在 Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) 在 System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) 在 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。
1.<ExecuteAsync>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func
1 编译器)在1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable1 source, LambdaExpression expression, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AnyAsync[TSource](IQueryable
1 source, Expression1 predicate, CancellationToken cancellationToken) at Call.Api.Handlers.CreateCallCommandHandler.Handle(CreateCallMappingCommand request, CancellationToken cancellationToken) in D:\Call.Api\Handlers\CreateCallCommandHandler.cs:line 40 at MediatR.Pipeline.RequestExceptionProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestExceptionProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestExceptionActionProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate 2.Handle(TRequest request, CancellationToken1 next) at MediatR.Pipeline.RequestExceptionActionProcessorBehavior
cancelToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestPostProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next) at MediatR.Pipeline.RequestPreProcessorBehavior
2.Handle(TRequest request, CancellationToken cancelToken, RequestHandlerDelegate1 next) at Call.Api.Controllers.CallsController.Create(CreateCallMappingCommand createCallMappingCommand) in D:\Call.Api\Controllers\CallsController.cs:line 52 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) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker 调用程序, 任务 lastTask, State next, Scope 范围, Object state, Boolean isCompleted) 在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed上下文)在 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() 的 Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) --- 上一个堆栈跟踪结束引发异常的位置 --- 在 Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker 调用程序,任务 lastTask,下一个状态,作用域范围,Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围)的对象状态,布尔值已完成)在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点端点,任务 requestTask,ILogger logger) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft。 AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文)g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围)在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点端点,任务 requestTask,ILogger 记录器)在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)g__Awaited|17_0(ResourceInvoker 调用程序,任务任务,IDisposable 范围)在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(端点端点,任务 requestTask,ILogger 记录器)在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)ILogger logger) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft .AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文)ILogger logger) 在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) 在 Microsoft .AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文)
模型调用公开课调用
{
public Guid CallId { get; set; }
public string Number { get; set; }
public int CustomerId { get; set; }
public Type Type { get; set; }
}
请求是一样的(只有没有CallId)
解决方案
.AnyAsync
不支持。作为替代方案,您可以使用.CountAsync()
并检查count > 0
;
令人难以置信的是团队会实施.CountAsync
而不是实施.AnyAsync
。
推荐阅读
- android - 我应该如何在新的 OpenCV 最新版本 4.0.1 中替换 FeatureDetector 功能?
- spring - 如何将 EOT 设置为 Spring 批处理文件处理的行分隔符
- apexcharts - 如何增加标签的宽度?
- google-apps-script - 来自 Gmail 中的 XLS 附件的 getValue()
- c# - 同一属性上的初始化 + 私有集访问器?
- apache-flink - Apache Flink 动态更新 sql 而无需重启
- swift - firebase 快照寿命
- sql-server - SQL Server - 在同一查询中获取带有过滤器和不带过滤器的计数(*)
- chart.js - 使用 ChartJs 时条形图不堆叠
- api - VueJS - 使用 axios 加载数据 - 数据为空