简体   繁体   English

实体框架在一个请求中加载多个集合

[英]Entity Framework load multiple collections in one request

In basic I'm looking for the simplest way to get an associated collection of an entity in one request. 在基本方面,我正在寻找在一个请求中获取实体的关联集合的最简单方法。

using (var context = new DbContext())
 {
   context.Users.Attach(user);
   context.Entry(user)
   .Collection(f => f.Followers)
   .Query()
   .Where(x => x.Whatever)
   //.Collection(r => r.Requests) doesn't work
   //.Collection(b => b.Blocks) doesn't work
   .Load();
 }

I'm aware that I could split these up into 3 different requests, but I'm wanting only one trip to the db. 我知道我可以将它们分为3个不同的请求,但是我只想一次访问数据库。

Alternatively I was able to pull this off: 另外,我能够做到这一点:

var user = context.Users
           .Include(f => f.Followers)
           .Include(r => r.Requests)

The problem with the approach above is I'm unable to filter the collections in the include with a .Where clause. 上面方法的问题是我无法使用.Where子句过滤include中的集合。

This question is very much: EF 6 filtering child collections 这个问题非常多: EF 6过滤子集合

As well as: Entity Framework Query multiple collections 以及: 实体框架查询多个集合

Neither of which were able to be pulled off with one request, so I'm guessing its not possible? 哪一个都无法通过一个请求完成,所以我猜想它不可能吗?

EF in its base does not support that. EF在其基础上不支持这一点。

You can split it into 3 separate queries and use some external library to do it in single round trip, for example Future Queries of EntityFramework.Extended nuget library. 您可以将其拆分为3个独立的查询,并使用一些外部库在一次往返中完成它,例如EntityFramework.Extended nuget库的Future Queries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM