简体   繁体   English

如何在LLBLGen中合并实体图?

[英]How to merge entity graphs in LLBLGen?

Let's assume I have 2 tables, A and B with 1-0..1 relation. 假设我有2个表,A和B的关系为1-0..1。 I use the Adapter approach. 我使用适配器方法。 I need to load a collection of A entities in one place, and then load all related B entities for all A entities later. 我需要在一个位置加载一组A实体,然后在以后加载所有A实体的所有相关B实体。 The reason to not use Prefetch is that in most cases I will not need to load B entities. 不使用预取的原因是,在大多数情况下,我不需要加载B实体。

I use LINQ everywhere, so I would like to do it the same way. 我到处都使用LINQ,所以我想用同样的方法。

The code I am trying to write looks like this: 我尝试编写的代码如下所示:

var linqMetadata = new LinqMetaData(adapter)
{
    ContextToUse = new Context()
};

var aCollection = linqMetadata.A.Where(/*some filter*/).ToList();

// ...

var bIds = aCollection.Select(x => x.BId);
var bCollection = linqMetadata.B.Where(x => bIds.Contains(x.BId)).ToList();

The problem is that bCollection and aCollection stay unlinked, ie all A entities have B = null and vice versa. 问题是, bCollectionaCollection留无关联,即所有A实体有B = null ,反之亦然。 I want these references to be set, and therefore the 2 graphs to be united into a single one. 我希望设置这些引用,因此将2个图合并为一个图。

I can join 2 collections using LINQ to Objects, but that's not elegant, and besides this might get much more complicated if both collections contain complex graph with interconnections that need to be established. 我可以使用LINQ将2个集合加入对象,但这并不优雅,此外,如果两个集合都包含需要建立互连的复杂图形,这可能会变得更加复杂。

I can write a prefetch from B to A, but that's one extra DB query that is completely unnecessary. 我可以编写从B到A的预取,但这是一个完全不必要的额外DB查询。

Is there an easy way to get these 2 graphs merged? 有没有简单的方法来合并这两个图?

Object relation graph is built automatically if you add related entities using assignment for master entity and AddRange for detail entities. 如果使用主实体的分配添加关联的实体,而对明细实体使用分配范围来添加相关实体,则将自动构建对象关系图。

Here's a pseudo-code sample: 这是一个伪代码示例:

foreach aEntity in aCollection
   aEntity.BEntity = bCollection.First(x=>x.Id == aEntity.bId);

OR

foreach bEntity in bCollection
   bEntity.AEntity.AddRange(aCollection.Where(x=>x.bId == bEntity.Id));

After doing this your object graph is complete. 完成此操作后,您的对象图就完成了。

Since you are already using a Context, you can use PrefetchPath and context to load the PrefetchPath later, that way your entities are always linked. 由于您已经在使用Context,因此可以在以后使用PrefetchPath和context加载PrefetchPath,这样始终可以链接您的实体。 Then you can run a Linq2Objects query to load in-memory all B's. 然后,您可以运行Linq2Objects查询以在内存中加载所有B。

var bs = aCollection.Select(x => x.B).ToList();

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

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