![](/img/trans.png)
[英]How Do You Reference a .NET Standard Library from a .NET Framework 4.5 Console Application in Visual Studio 2017?
[英]Include method from Entity Framework from in .NET Framework 4.5 is incompatible with .NET Standard 2.0
該類最初是在.NET Framework 4.5中編寫的,現在我將其轉換為.NET Standard 2.0。 但是,include方法的行為不再相同。 我收到以下錯誤:
“ IQueryable”不包含“ Include”的定義,並且找不到包含可接受類型為“ IQueryable”的第一個參數的可訪問擴展方法“ Include”(您是否缺少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;
}
包含在Microsoft.EntityFrameworkCore命名空間和Microsoft.EntityFrameworkCore.dll程序集中:
EntityFrameworkQueryableExtensions.Include方法
但是在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 });
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.