如何在Entity Framework Core中传递具有多个级别的lambda'include'? [英] How to pass lambda 'include' with multiple levels in Entity Framework Core?

查看:3049
本文介绍了如何在Entity Framework Core中传递具有多个级别的lambda'include'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储库,可以获取'include'的lambda表达式。

I have a repository that gets a lambda expression for 'include'.

public TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includePaths)
    {
        return Context.Set<TEntity>().Includes(includePaths).FirstOrDefault(predicate);
    }

在以前的EF版本中,我在服务层使用它,如:

In previous versions of EF I used it in services layers like:

var plan = _unitOfWork.PlanRepository
            .FirstOrDefault(
                p => p.Id == id, 
                include => include.PlanSolutions.Select(ps => ps.Solution)
            );

其中'PlanSolutions'是一个集合,'Solution'是一个嵌套在'PlanSolution'中的属性。

Where 'PlanSolutions' is a collection and the 'Solution' is a property nested from 'PlanSolution'.

但是现在这段代码出错:

But now this code gets an error:


InvalidOperationException:属性表达式'include => {from
PlanSolutions ps in [include] .PlanSolutions select [ps] .Solution}'
无效。表达式应代表属性访问:'t =>
t.MyProperty'。有关包含相关数据的详细信息,请参阅
http://go.microsoft。 com / fwlink /?LinkID = 746393

现在看来我不能使用'Select'方法获得多个级别包括,但我也不能使用Microsoft建议的'ThenInclude'方法,因为查询本身位于服务器无法访问的存储库内。
有没有办法治愈它?

Now it seems that I can't use 'Select' method for getting multiple levels include, but I also can't use 'ThenInclude' method that Microsoft suggests, because the query by itself located inside of the repository where service doesn't have an access to. Is there any way to heal it?

推荐答案

实体框架核心为更易于理解的API牺牲了参数化的简易性。实际上,在EF6中,将多级包含表达式传递给方法要容易得多。在ef-core中几乎是不可能的。

Entity Framework core sacrificed ease of parametrization for a more comprehensible API. Indeed, in EF6 it was much easier to pass multi-level Include expressions to a method. In ef-core that's virtually impossible.

但接受属性路径为字符串的 Include 方法仍然存在,所以如果我们可以将旧式多级包含表达式转换为路径,我们可以将路径提供给这个基于字符串的包含

But the Include method accepting a property path as string still exists, so if we can convert the old-style multi-level Include expression to a path, we can feed the path into this string-based Include.

幸运的是,这正是EF6引擎盖下发生的事情。而且由于EF6是开源的,我不必重新发明轮子,但可以轻松借用他们的代码来实现我们想要的。结果是一个扩展方法 AsPath ,它返回一个lambda表达式作为属性路径。您可以在方法中使用它将 includes 参数转换为一系列字符串,您可以通过这些字符串添加包含秒。例如,表达式......

Fortunately, this is exactly what happened under the hood in EF6. And since EF6 is open source, I didn't have to reinvent the wheel but could easily borrow their code to achieve what we want. The result is an extension method AsPath that returns a lambda expression as a property path. You can use it inside your method to convert the includes parameter to a sequence of strings by which you can add the Includes. For example, the expression ...

 include => include.PlanSolutions.Select(ps => ps.Solution)

...将被转换为 PlanSolutions.Solution

如上所述:EF6为核心部分提供信用。唯一的主要修改是我的方法在两个最常尝试的不支持的功能中抛出异常:过滤和订购包含。 (仍然不支持ef-core)。

As said: credits to EF6 for the core part of the source. The only major modification is that my method throws exceptions in two of the most commonly attempted unsupported features: filtering and ordering an Include. (Still not supported in ef-core).

public static class ExpressionExtensions
{
    public static string AsPath(this LambdaExpression expression)
    {
        if (expression == null) return null;

        var exp = expression.Body;
        string path;
        TryParsePath(exp, out path);
        return path;
    }

    // This method is a slight modification of EF6 source code
    private static bool TryParsePath(Expression expression, out string path)
    {
        path = null;
        var withoutConvert = RemoveConvert(expression);
        var memberExpression = withoutConvert as MemberExpression;
        var callExpression = withoutConvert as MethodCallExpression;

        if (memberExpression != null)
        {
            var thisPart = memberExpression.Member.Name;
            string parentPart;
            if (!TryParsePath(memberExpression.Expression, out parentPart))
            {
                return false;
            }
            path = parentPart == null ? thisPart : (parentPart + "." + thisPart);
        }
        else if (callExpression != null)
        {
            if (callExpression.Method.Name == "Select"
                && callExpression.Arguments.Count == 2)
            {
                string parentPart;
                if (!TryParsePath(callExpression.Arguments[0], out parentPart))
                {
                    return false;
                }
                if (parentPart != null)
                {
                    var subExpression = callExpression.Arguments[1] as LambdaExpression;
                    if (subExpression != null)
                    {
                        string thisPart;
                        if (!TryParsePath(subExpression.Body, out thisPart))
                        {
                            return false;
                        }
                        if (thisPart != null)
                        {
                            path = parentPart + "." + thisPart;
                            return true;
                        }
                    }
                }
            }
            else if (callExpression.Method.Name == "Where")
            {
                throw new NotSupportedException("Filtering an Include expression is not supported");
            }
            else if (callExpression.Method.Name == "OrderBy" || callExpression.Method.Name == "OrderByDescending")
            {
                throw new NotSupportedException("Ordering an Include expression is not supported");
            }
            return false;
        }

        return true;
    }

    // Removes boxing
    private static Expression RemoveConvert(Expression expression)
    {
        while (expression.NodeType == ExpressionType.Convert
               || expression.NodeType == ExpressionType.ConvertChecked)
        {
            expression = ((UnaryExpression)expression).Operand;
        }

        return expression;
    }
}

这篇关于如何在Entity Framework Core中传递具有多个级别的lambda'include'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆