从实体列表开始,通过关联需要所有依赖实体,是否有一种方法可以使用相应的导航-属性加载所有子实体的一个db-往返?即。通过导航属性生成单个WHERE fkId IN (...)语句?
更多细节
我找到了这些方法让孩子们:
- Not good since the db will have to find the main set every time and join to get the requested data.
var children = parentArray.Select(p => p.Children).Distinct()- This is slow since it will generate a select for every main-entity.
- Creates duplicate objects since each set of children is created independetly.
var foreignKeyIds = parentArray.Select(p => p.Id).ToArray();
var children = Children.Where(d => foreignKeyIds.Contains(d.Id))
Linq然后生成所需的"WHERE foreignKeyId IN (.)“-clause。- This is fast but only possible for 1:\*-relations since linking-tables are mapped away.
- Removes the readablity advantage of EF by using Ids after all
- The navigation-properties of type EntityCollection<T> are not populated
- Alledgedly joins everything included together and returns one giant flat result.
- Have to decide up front which data to use
有什么方法可以使2的简单性达到3的性能吗?
发布于 2011-05-04 07:42:22
您可以将父对象附加到上下文中,并在需要时获取子对象。
foreach (T parent in parents) {
_context.Attach(parent);
}
var children = parents.Select(p => p.Children);编辑:用于附加多个,只需迭代。
发布于 2011-11-15 08:14:13
我认为找一个好的答案是不可能的,或者至少不值得麻烦。相反,像脱衣舞这样的微ORM提供了很大的好处,消除了在sql列和对象属性之间映射的需要,并且不需要首先创建模型。另外,只需编写所需的sql,而不是理解要编写哪些linq才能生成它。不过,我们会想念IQueryable<T>的。
https://stackoverflow.com/questions/3996959
复制相似问题