c# - 可查询的没有获得导航属性
问题描述
我不知道我在这里没有得到什么,但导航属性没有使用 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);
推荐阅读
- javascript - 我的局部变量没有更新全局变量?
- python - 为什么我的网页抓取结果在循环浏览每个网页后不会打印?
- javascript - node.js 发布方法 req.body 未定义,即使使用 body-parser
- javascript - 如何使用 JavaScript 获取操作系统名称和版本?
- flutter - Flutter 在 Column 内使用 TabBar 而不是 AppBar
- mongodb - 查询元素,如果它们的对象数组具有重复元素的属性
- c# - 即使用户已登录,httpcontext 也将用户身份返回为 null,尝试为 hangfire 设置 authenticationfilter
- c - 无法将 Mingw-w64 正确添加到 Windows 8.1 上的路径
- python - 如何在不修改常量值和数据类型的情况下将结果存储在变量中
- java - 返回单链表的大小但不能使用head