简体   繁体   English

当不存在显式关系时,使用实体框架请求父子对象集合

[英]Use Entity Framework to request parent-child object collection when no explicit relationship exists

I have 2 classes, see below They are POCO classes which we use to access the database, we use code first approach and configure all mapping between entities and DB tables explicitly, using ApplicationEntityConfiguration. 我有2个类,请参见下文,它们是POCO类,我们使用它们来访问数据库,我们使用代码优先方法,并使用ApplicationEntityConfiguration显式配置实体和DB表之间的所有映射。

 public class Group
{
    public string Code { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Item> Items { get; set; }
}

public class Item
{
    public string Code { get; set; }
    public string Name { get; set; }
    public virtual Group ParentGroup { get; set; }
}

The relatioship beteen Groups and Childs is not explicit. 组和子级之间的关系不明确。 There is no foreign key. 没有外键。 Logically relationship is defined by Code, Group should contain all items which have codes staring with the same code as parent group 从逻辑上讲,关系是由代码定义的,组应包含所有具有与父组相同的代码开头的代码的项目

Context.Items.Where(x => x.Code.StartsWith(parentGroup.Code));

I need to return to caller method a collection of all Groups in the database with all childs for each group. 我需要返回调用方方法数据库中所有组的集合,其中每个组都有所有子项。 When requesting the data I can request Context.Groups and for each Group request Items using Where. 请求数据时,我可以请求Context.Groups,并且对于每个Group,都可以使用Where来请求Item。 I have more then 50 groups and more then 1000 items in the database So such manual approach would result in more then 50 queries to the database and will have performance impact 我在数据库中有50多个组和1000多个项目,因此这种手动方法将导致对数据库的查询超过50个,并且会对性能产生影响

More optimal solution would be to request all Group in one collection, then all Item in another collection and then when all those data in memory, fill a collection of Items for each group... then it will not hit DB a lot. 更好的解决方案是请求一个集合中的所有Group,然后再请求另一个集合中的所有Item,然后在内存中存储所有这些数据时,为每个组填充一个Items集合……那么这不会对数据库造成很大的影响。

So, while I able to achive the goal in general, I wonder if there is a more elegant solution where we can declarativly tell Enity Framework that relationship between groups and items is established using the expression .Where(x => x.Code.StartsWith(parentGroup.Code)) and then once it is declared, developer can use Include() when he wants to Load groups and items together or rely on Lazy loading that can load child items when they will be accessed 因此,虽然我总体上可以实现目标,但我想知道是否存在一个更优雅的解决方案,在该解决方案中,我们可以声明性地告诉Enity Framework使用表达式.Where(x => x.Code.StartsWith (parentGroup.Code)),然后在声明后,开发人员可以在希望将组和项目一起加载时使用Include(),也可以依赖于懒惰加载来在访问子项时加载它们

There is no better way to do this than your second approach. 没有比第二种方法更好的方法了。 This is because Entity Framework is only able to load collections that have a foreign key relationship to the PK of the parent. 这是因为实体框架只能加载与父级PK具有外键关系的集合。 There is no support for associations to unique indexes yet. 尚不支持与唯一索引的关联。

Likewise, the process you're referring to, relationship fixup , can only work when the association is mapped. 同样,您所指的过程“ 关系修正”仅在映射关联时才有效。 But in your case it can't be mapped, so EF can't populate the collections itself. 但是在您的情况下,它无法映射,因此EF无法填充集合本身。

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

相关问题 如何使用实体框架保持父子C#关系 - How to hold Parent-Child C# relationship with Entity Framework Fluent NHibernate - 当模型中不存在明确的parent-&gt; child关系时,Cascade删除子对象 - Fluent NHibernate - Cascade delete a child object when no explicit parent->child relationship exists in the model 实体框架自参考-父子 - Entity Framework self referencing - Parent-Child 实体框架:如何在自引用父子关系上指定外键列的名称? - Entity Framework: How to specify the name of Foreign Key column on a self-referencing Parent-Child relationship? 使用实体框架或实体框架核心删除父子关系 - Deleting parent-child relation with Entity Framework or Entity Framework Core 商店亲子关系 - store parent-child relationship ASP.NET MVC 父子视图模型和实体框架 - ASP.NET MVC Parent-Child with ViewModel and Entity Framework 如何在 Entity Framework Core 2.2 中使用预先加载策略实现自递归父子关系数据加载? - How to implement self-recursive parent-child relationship data loading with eager loading policy in Entity Framework Core 2.2? 实体框架:递归更新Parent-Child给我重复 - Entity framework : Recursive update Parent-Child gives me duplicates 在实体框架中删除父子表中的行 - remove row in parent-child table in Entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM