I'm using Entity Framework 4.1 and I have a one to many relationship.

When I query the lazy loaded ICollection<T> on the one side of the relationship the whole recordset is returned and it doesn't defer the execution like when I'm querying direct from the Repository IQueryable interface.

Is there any way to make this use deferred execution so I can do a query like

Model.Childs.Where(x => !x.Deleted.HasValue).Skip(10).Take(5);

Thanks in advance,

Tom.

有帮助吗?

解决方案

That is principle of lazy loading in EF. Your navigation property is defined in your code and any LINQ query defined on that property is LINQ-to-Objects - it is not translated to SQL. Lazy loading always loads whole collection (as well as eager loading). When you query your repository you are querying IQueryable and using LINQ-to-Entities which is translated to SQL.

As workaround use explicit loading:

dbContext.Entry(Model).Collection(m => m.Childs)
                      .Query()
                      .Where(c => !c.Deleted.HasValue)
                      .Skip(10)
                      .Take(5)
                      .Load();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top