查询一个实体的子集的实体框架4.1 [英] Querying an Entity's child collections in Entity Framework 4.1

查看:123
本文介绍了查询一个实体的子集的实体框架4.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我从IDbSet在我的DbContext查询实体。我现在要遍历每个实体和查询他们的孩子的集合,这是在实体ICollection的定义。它是正确的调用AsQueryable已()上的子集合并在运行我的LINQ查询?如果是这样,我的疑问是LINQ到对象或不填充的EF集合对象实现IQueryable的是进入到数据库?

I have a set of entities that I have queried from IDbSet in my DbContext. I now want to iterate over each entity and query their child collections, which are defined in the entity as ICollection. Is it correct to call AsQueryable() on the child collections and run my linq query on that? If so, will my queries be linq-to-objects or does the collection object populated by EF implement IQueryable that goes to the database?

感谢有这方面的见解。

推荐答案

这全取决于你的实体定义,如果延迟加载功能。您的查询到 IDbSet 将LINQ到实体。如果延迟加载启用访问加载的实体的每个导​​航属性​​会触发数据库查询,这将加载所有相关实体。 AsQueryable已不起作用在这里你将仍然执行LINQ到对象查询所有已加载数据。在这种情况下实在是更好的方法使用预先加载,并与主要实体加载相关实体在一起:

It whole depends on how your entities are defined and if lazy loading is enabled. Your query to IDbSet will be linq-to-entities. If lazy loading is enabled accessing each navigation property of the loaded entity will trigger database query which will load all related entities. AsQueryable has no effect here you will still execute linq-to-objects query on all loaded data. In such scenario it is really better approach to use eager loading and load your related entities together with the main entity:

var query = context.YourEntitySet.Include(e => e.YourNavProperty);

在某些情况下,执行这个查询可以在内部产生<一href="http://stackoverflow.com/questions/5521749/how-many-include-i-can-use-on-objectset-in-entityframework-to-retain-performance/5522195#5522195">very大结果集。

In some cases executing this query can internally produce very big result sets.

如果你有很多相关的实体,你真的要加载只有一些很小的子集,你可以用下面的办法:

If you have a lot of related entities and you really want to load only some very small subset you can use following approach:

context.Entry(yourLoadedMainEntity)
       .Collection(e => e.YourNavProperty)
       .Query()
       .Where(...)
       .Load();

这迫使EF加载相关实体的唯一子集LINQ到实体的方式。您还必须执行此为每个加载的主要实体。执行这么多的疑问是很慢的。它仍然是<一个href="http://stackoverflow.com/questions/5070013/select-n1-in-next-entity-framework/5070644#5070644">N+1问题。

This the way to force EF to load only subset of related entities with linq-to-entities. You still have to execute this for each loaded main entity. Executing so many queries is very slow. It is still N+1 problem.

另外,最复杂的优化加载在另一个查询中的单个查询各主要机构和所有相关实体:

Another and most complex optimization is loading all main entities in the single query and all related entities in another query:

var query = context.YourEntitySet;
var relatedQuery = from e in context.YourRelatedEntitySet
                   join m in context.YourEntitySet on e.MainId equals m.Id
                   where ...
                   select e;

在您执行这两个查询实体框架应确保导航属性正确填充相关的实体,但这仅在延迟加载是关闭的,但实体上下文跟踪(以及我从来没有试图与的DbContext API,但它的工作ObjectContext的API)。

Once you execute both queries Entity framework should ensure that navigation properties are correctly filled with related entities but this works only when lazy loading is turned off but entities are tracked by context (well I never tried this with DbContext API but it worked with ObjectContext API).

这篇关于查询一个实体的子集的实体框架4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆