[英]Linq expression could not be translated Error Entity Framework Core 3
我在一個項目中,在數據訪問方面遇到了一些問題。 我使用 EF 核心並且我有這些堆棧跟蹤。
public static void Main(){
EntityServiceBase<Product> manager = new EntityServiceBase(new EFProductDal());//efproductdal for to access database
Product result = manager.GetByPrimaryKey(5);
}
在代碼中 IEntity 是一個確定數據庫對象的空接口
IEntityRepostiory 具有訪問數據庫的 EF 代碼,該代碼顯示在屬於
PrimaryKeyComparable 具有比較功能來檢查對象的主鍵是否等於給定的鍵
using System;
using System.Collections.Generic;
using ECommercial.Core.DataAccess;
using ECommercial.Core.Entities;
namespace ECommercial.Core.Business
{
public abstract class EntityServiceBase<TEntity> : IService<TEntity>
where TEntity :class,IEntity, new() // IENTITY IS AN EMPTY INTERFACE TO JUST DETERMINE DATABASE ENTITIES
{
private IEntityRepository<TEntity> _entityRepository;
public EntityServiceBase(IEntityRepository<TEntity> entityRepository)
{
_entityRepository = entityRepository;
}
public TEntity GetByPrimaryKey(Object key)
{
PrimaryKeyComparable primaryKeyComparable = new PrimaryKeyComparable();
return _entityRepository.Get(o=>primaryKeyComparable.comparePrimaryKey<TEntity>(o,key));
}
}
}
在代碼上下文是 EF dbcontext
過濾器被賦予過濾器,即 o=>primaryKeyComparable.comparePrimaryKey(o,key) 第二個代碼文件。 錯誤發生在 .SingleOrDefault(filter) 部分。 如果我刪除它,功能運行良好。
public TEntity Get(Expression<Func<TEntity, bool>> filter)
{
using (var context=new TContext()){
TEntity result = context.Set<TEntity>().SingleOrDefault(filter);
return result;
}
}
public bool comparePrimaryKey<TEntity>(TEntity entity,Object value)
where TEntity:class,IEntity,new()
{
Type entityType = typeof(TEntity);
FieldInfo[] fields = entityType.GetFields();
FieldInfo found =null;
foreach(var field in fields){
Attribute attribute=field.GetCustomAttribute(typeof(PrimaryKeyFieldAttribute));
if(attribute!=null){
found=field;
break;
}
}
if(found!=null){
Type foundDeclaringType= found.DeclaringType;
Type valueDeclaringType = value.GetType().DeclaringType;
if((foundDeclaringType.IsSubclassOf(valueDeclaringType)) ||valueDeclaringType.IsSubclassOf(foundDeclaringType) || valueDeclaringType==foundDeclaringType)
{
return entity.Equals(value);
}
throw new InvalidCastException("Given entity's primary key must have same or child-base related declaration type with key object compared");
}
throw new CustomAttributeFormatException("The Entity Has Any field has PrimaryKeyFieldAttribute attribute. Must be PrimaryKeyAttribute on primary key field.");
}
錯誤發生在 .SingleOrDefault(filter) 部分的第三個文件
Exception has occurred: CLR/System.InvalidOperationException
'System.InvalidOperationException' türünde özel durum Microsoft.EntityFrameworkCore.dll öğesinde oluştu, fakat kullanıcı kodunda işlenmedi: 'The LINQ expression 'DbSet<Product>
.Where(p => new PrimaryKeyComparable().comparePrimaryKey<Product>(
entity: p,
value: __key_0))' 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.'
konum Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
konum Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
konum Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
konum System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
konum System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
konum Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
konum Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
konum System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
konum System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
konum Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
konum Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
konum Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
konum Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
konum Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
konum Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
konum Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
konum Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
konum System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
konum ECommercial.Core.DataAccess.EntitiyFramework.EFIEntityRepositoryBase`2.Get(Expression`1 filter) C:\Users\nihaSWin\Desktop\ECommercial\ECommercial.Core\DataAccess\EntitiyFramework\EFIEntityRepositoryBase.cs içinde: 36. satır
konum ECommercial.Core.Business.EntityServiceBase`1.GetByPrimaryKey(Object key) C:\Users\nihaSWin\Desktop\ECommercial\ECommercial.Core\Business\EntityServiceBase.cs içinde: 26. satır
konum ECommercial.MVC.Startup..ctor(IConfiguration configuration) C:\Users\nihaSWin\Desktop\ECommercial\ECommercial.MVC\Startup.cs içinde: 19. satır
所以看起來你正在嘗試使用反射來確定主鍵和基於它的查詢。 您不能直接在表達式中使用這樣的方法,並期望 EF 解開它的作用並將其轉換為 sql。
但是,您可以使用反射來生成表達式並直接使用它。
public Expression<Func<TEntity, bool>> comparePrimaryKey<TEntity>(object value)
{
var parm = Expression.Parameter(typeof(TEntity), "e");
var objType = value.GetType();
return Expression.Lambda<Func<TEntity, bool>>(
typeof(TEntity)
.GetFields()
.Where(f => f.GetCustomAttribute(typeof(PrimaryKeyFieldAttribute)) != null)
.Select(f => (Expression)Expression.Equal(
Expression.MakeMemberAccess(parm, f),
Expression.Constant(
(objType == f.FieldType)
? value
: objType.GetField(f.Name).GetValue(value),
f.FieldType)
))
.Aggregate((l, r) => Expression.AndAlso(l, r)),
parm);
}
//...
return _entityRepository.Get(primaryKeyComparable.comparePrimaryKey<TEntity>(key));
雖然我建議使用 dbContext.Model 元數據來發現主鍵,而不是依賴於反射和屬性約定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.