我不知道我在这里没有得到什么,但是没有使用 Inlucde 方法加载导航属性。
public IEnumerable<T> GetAll<T>() where T : class
{
List<string> navigationProperties =
GetNavigationProperties<T>();
var query = _context.Set<T>().AsQueryable();
foreach (var navigationProperty in navigationProperties)
{
query.Include(navigationProperty);
}
return query.ToList();
}
private static List<string> GetNavigationProperties<T>() where T : class
{
return typeof(T).GetProperties()
.Where(x => x.GetCustomAttributesData()
.Any(p => p.AttributeType == typeof(NavigationPropertyAttribute)))
.Select(n => n.Name).ToList();
}
NavigationPropertyAttribute 是自定义属性, GetNavigationProperties() 方法返回我在调试期间看到的指定类型的所有导航属性的列表。
最佳答案
您必须分配返回值 - Include()
不是 void 类型
query = query.Include(navigationProperty);
关于c# - IQueryable<T> 没有获取导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50967787/