EF - 为什么不包括属性工作

发布于 12-04 07:52 字数 709 浏览 0 评论 0原文

所以我的课程看起来像这样。

public class User {
    public virtual IList<Member> Members {get;set;}
}

public class Member {
    public virtual AnotherTable Another {get;set;}
}

public class AnotherTable {
    public string Name {get;set;}
}

当我直接对 DataContext 执行查询时,包含有效,但是当我对成员 IList 执行 AsQueryable() 时,包含不起作用。

有没有办法在延迟加载的属性(例如上面的 Members 属性)上具有 Include/Eager 功能,或者我是否始终必须通过 DataContext 才能获取该功能?

User.Members.AsQueryable().Include(a => a.Another).ToList() // <-- nada, no way Jose
_db.Members.Include(m => m.Another).ToList() // <-- all good in the neighborhood

我问这个问题是因为 1 个 sql 查询与 100 个查询获得相同结果可能存在巨大差异。

提前致谢。

So I have classes that looks like this.

public class User {
    public virtual IList<Member> Members {get;set;}
}

public class Member {
    public virtual AnotherTable Another {get;set;}
}

public class AnotherTable {
    public string Name {get;set;}
}

When I perform the query directly against the DataContext the Include works, but when I do an AsQueryable() on the IList of members the include doesn't work.

Is there a way to have Include/Eager functionality on lazy loaded properties, such as the Members property above, or do I always have to go through the DataContext to get that feature?

User.Members.AsQueryable().Include(a => a.Another).ToList() // <-- nada, no way Jose
_db.Members.Include(m => m.Another).ToList() // <-- all good in the neighborhood

I ask cause it can be a huge difference of 1 sql query vs. 100 queries for something result equivalent.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-柠檬树下少年和吉他2024-12-11 07:52:28

AsQueryable 不会使其成为 linq-to-entities 查询。它仍然是 List 之上的 Linq-to-object 查询。 List 不知道如何处理 Include - 只有 DbQuery 知道它,因此您必须获取 DbQuery

var entry = context.Entry(user);
entry.Collection(u => u.Member).Query().Include(m => m.Another).Load();

AsQueryable doesn't make it linq-to-entities query. It is still Linq-to-object query on top of List. List doesn't know how to handle Include - only DbQuery knows it so you must get DbQuery:

var entry = context.Entry(user);
entry.Collection(u => u.Member).Query().Include(m => m.Another).Load();
梦里梦着梦中梦2024-12-11 07:52:28

您必须遍历 DbContext 才能使 Include() 工作。您可以将其抽象到存储库中,但您仍然需要将 Include() 表达式传递到底层上下文。

    private IQueryable<T> GetQuery<T>(params Expression<Func<T, object>>[] includeProperties) where T : class
    {
        IQueryable<T> query = _db.Set<T>();

        if (includeProperties != null)
        {
            foreach (Expression<Func<T, object>> expression in includeProperties)
            {
                query = query.Include(expression);
            }
        }

        return query;
    }

You'll have to go through the DbContext in order for Include() to work. You could abstract it into a Repository, but you'll still need to pass your Include() expression to your underlying context.

    private IQueryable<T> GetQuery<T>(params Expression<Func<T, object>>[] includeProperties) where T : class
    {
        IQueryable<T> query = _db.Set<T>();

        if (includeProperties != null)
        {
            foreach (Expression<Func<T, object>> expression in includeProperties)
            {
                query = query.Include(expression);
            }
        }

        return query;
    }
め可乐爱微笑2024-12-11 07:52:28

我也面临着同样的问题。

我仅添加了参考系统。data.entity&amp;使用以下名称空间:

   using System.Data.Entity;

您可以尝试。

I also faced same problem.

I solved this just adding the reference System.Data.Entity & use following namespace:

   using System.Data.Entity;

You can try with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文