.NET Framework 4.5中的Entity Framework中的include方法与.NET Standard 2.0不兼容

时间:2018-10-17 11:44:44

标签: c# entity-framework microservices .net-framework-version .net-standard-2.0

最初,此类是用.NET Framework 4.5编写的,现在我将其转换为.NET Standard 2.0。但是,include方法的行为不再相同。我收到以下错误:

  

“ IQueryable”不包含“ include”的定义,并且没有   可访问的扩展方法“ include”,它接受第一个参数   可以找到“ IQueryable”类型(您是否缺少using指令或   组装参考?)

正在使用的库:

using Microservices.LibCore.Core;
using Microservices.LibCore.Core.Base.Models;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Reflection;

public static IQueryable<T> IncludeRelated<T>(this IQueryable<T> originalQuery, int maxLevel = 2, bool includeCollections = false)
    {
        if (Config.get<bool>("EntityUtil_IncludeRelatedCollectionsAlways", false))
        {
            includeCollections = true;
        }

        var includeFunc = IncludeRelatedRecursive(typeof(T), "", 1, maxLevel, includeCollections);

        if (includeFunc != null)
        {
            return (IQueryable<T>)includeFunc(originalQuery);
        }
        else
        {
            return originalQuery;
        }
    }

private static Func<IQueryable, IQueryable> IncludeRelatedRecursive(Type type, string root, int level, int maxLevel, bool includeCollections = false)
    {
        if (level > maxLevel)
        {
            return null;
        }

        if (includeCollections)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ICollection<>))
            {
                type = type.GetGenericArguments()[0];
            }
        }
        Func<IQueryable, IQueryable> includeFunc = null;

        foreach (var prop in type.GetProperties()

            .Where(p => Attribute.IsDefined(p, typeof(ForeignKeyAttribute)) &&
            !Attribute.IsDefined(p, typeof(JsonIgnoreAttribute))))
        {
            var includeChildPropFunc = IncludeRelatedRecursive(prop.PropertyType, root + prop.Name + ".", level + 1, maxLevel, includeCollections); //propertiesChecked

            if (includeChildPropFunc != null)
            {
                includeFunc = Compose(includeFunc, includeChildPropFunc);
            }
            else
            {
                Func<IQueryable, IQueryable> includeProp = f => f.Include(root + prop.Name);

                includeFunc = Compose(includeFunc, includeProp);
            }
        }
        return includeFunc;
    }

1 个答案:

答案 0 :(得分:1)

包含在Microsoft.EntityFrameworkCore命名空间和Microsoft.EntityFrameworkCore.dll程序集中:

EntityFrameworkQueryableExtensions.Include Method

但是在EF Core中,它需要IQueryable<T>,而不是IQueryable。由于您使用反射遍历实体图(因此没有编译时实体类型T),因此必须使用反射来调用Include。像这样:

    public static System.Linq.IQueryable Include(this System.Linq.IQueryable source, string navigationPropertyPath)
    {
        var entityType = source.GetType().GetGenericArguments().Single();

        var includeMethodGenericDefinition = typeof(Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions).GetMethods()
                                                .Where(m => m.Name == "Include")
                                                .Where(m => m.GetParameters()[1].ParameterType == typeof(string))
                                                .Single();
        var includeMethod = includeMethodGenericDefinition.MakeGenericMethod(entityType);

        return (IQueryable)includeMethod.Invoke(null, new object[] { source, navigationPropertyPath });

    }