c# - IQueryable<T> 没有获取导航属性

标签 c# entity-framework

我不知道我在这里没有得到什么,但是没有使用 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/

相关文章:

c# - Xamarin 错误 : Unable to open text symbol file obj\Debug\R. txt:权限被拒绝 "R.txt: Permission denied"

c# - 如何更改过滤器和外键列在 DynamicData 站点中的呈现方式?

c# - 每当更新派生类时配置基类属性的自动更新 EF Core 2.0

c# - 没有 App.config 的 SQLite Entity Framework

c# - 添加对象后 Entity Framework 不加载导航属性

c# - ComputedVerticalScrollBarVisibility 触发器在使用绑定(bind)/转换器时不起作用

c# - 防止一种形式在传递某些数据时打开两次

c# - 向 UI 添加内容时如何阻止 UI 被锁定

c# - 在 WCF 测试客户端中输入字节数组的值

c# - 使用通用存储库更新多对多关系