微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linq表达式无法翻译错误实体框架核心3

如何解决Linq表达式无法翻译错误实体框架核心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;
            }
        }

ComparePrimaryKey函数

public bool comparePrimaryKey<TEntity>(TEntity entity,Object value)
            where TEntity:class,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)PART的第三个文件出现错误

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元数据来发现主键,而不要依赖反射和属性约定。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。