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

Include method from Entity Framework from in .NET Framework 4.5 is incompatible with .NET Standard 2.0

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

'IQueryable' does not contain a definition for 'Include' and no accessible extension method 'Include' accepting a first argument of type 'IQueryable' could be found (are you missing a using directive or an assembly reference?)

正在使用的库:

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;
    }

包含在 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 });

    }