实体框架嵌套导航属性仅计数(Entity Framework nested navigation properties count only)

编程入门 行业动态 更新时间:2024-10-09 08:27:50
实体框架嵌套导航属性仅计数(Entity Framework nested navigation properties count only)

我有三个模型类

public class Item1{ public int Id; public List<Item2> Item2List { get; set; } } public class Item2{ public int Id; //this is the FK public int Item1Id {get;set;} public Item1 Item1 {get;set;} //Not in db. Ignored field in EntityTypeConfiguration public int Item3Count; public List<Item3> Item3List { get; set; } } public class Item3{ public int Id; //this is the FK public int Item2Id {get;set;} public Item2 Item2 {get;set;} }

我想返回Item1的列表以及相关Item2的列表,并加载与Item2相关联的Item3List的COUNT而不加载Item3List。

这就是我现在正在做的事情:

public IEnumerable<Item1> GetItems() { return base.Query().Include(item1 => item1.Item2List.Select(item2 => item2.Item3List)).ToList(); }

这将返回所有3个对象Item1,Item2和Item3的列表。 但我只需要Item3Count中Item3List的计数,而不是整个Item3List列表。 我怎样才能做到这一点? 我在下面尝试了这个,但它会抛出错误。

return base.Query().Include(item1 => item1.Item2List.Select(item2 => new Item2 { Item3Count = item2.Item3List.Count() })).ToList();

Include路径表达式必须引用在类型上定义的导航属性。 使用虚线路径作为参考导航属性,使用Select运算符作为集合导航属性。 参数名称:路径

I have three model classes

public class Item1{ public int Id; public List<Item2> Item2List { get; set; } } public class Item2{ public int Id; //this is the FK public int Item1Id {get;set;} public Item1 Item1 {get;set;} //Not in db. Ignored field in EntityTypeConfiguration public int Item3Count; public List<Item3> Item3List { get; set; } } public class Item3{ public int Id; //this is the FK public int Item2Id {get;set;} public Item2 Item2 {get;set;} }

I want to return the list of Item1 along with list of associated Item2, and load the COUNT of Item3List associated with Item 2 without loading the Item3List.

Here is what I am doing right now:

public IEnumerable<Item1> GetItems() { return base.Query().Include(item1 => item1.Item2List.Select(item2 => item2.Item3List)).ToList(); }

This returns me the list of all 3 objects Item1, Item2 and Item3. But I only need the count of Item3List in Item3Count, and not the entire Item3List list. How can I achieve that? I tried this below, but it throws error.

return base.Query().Include(item1 => item1.Item2List.Select(item2 => new Item2 { Item3Count = item2.Item3List.Count() })).ToList();

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path

最满意答案

你想要的是不可能的。 当然,您无法在EF LINQ查询中填充未映射的属性,因为这是不映射它的想法。 但你已经知道了。

你真正想做的是这样的事情:

context.Item1s.Select(item1 => new Item1 { Id = item1.Id, Item2s = item1.Item2List.Select(item2 => new Item2List { Id = item2.Id, Item3Count = item2.Item3List.Count() }) })

但EF不允许您在EF查询中构造实体对象。

替代方案并不吸引人。

你可以建立一个匿名类型的结构......

context.Item1s.Select(item1 => new { Item1 = item1, Item2s = item1.Item2List.Select(item2 => new { Item2 = item2, Item3Count = item2.Item3List.Count() }) })

...并使用它来构造Item1对象的列表,每个对象的Item2List都是Item2的,具有Item3Count值。

更好,但仍然不接近理想的是使用AutoMapper并将实体映射到DTO:

Mapper.CreateMap<Item1,Item1Dto>(); Mapper.CreateMap<Item2,Item2Dto>();

您可以使用AutoMapper的展平功能填充Item3Count 。 为此, Item2Dto应具有属性Item3ListCount ,AutoMapper将其转换为Item3List.Count() 。

What you want is not possible. Of course you can't populate a not-mapped property in an EF LINQ query, because that's the idea of not mapping it. But you already knew that.

What you'd really like to do is something like this:

context.Item1s.Select(item1 => new Item1 { Id = item1.Id, Item2s = item1.Item2List.Select(item2 => new Item2List { Id = item2.Id, Item3Count = item2.Item3List.Count() }) })

But EF doesn't allow you to construct an entity object in an EF query.

The alternatives are not appealing.

You could build a structure of anonymous types ...

context.Item1s.Select(item1 => new { Item1 = item1, Item2s = item1.Item2List.Select(item2 => new { Item2 = item2, Item3Count = item2.Item3List.Count() }) })

... and use this to construct a list of Item1 objects, each having their Item2Lists of Item2s with Item3Count values.

Better, but still not close to what would be ideal, is to use AutoMapper and map the entities to DTOs:

Mapper.CreateMap<Item1,Item1Dto>(); Mapper.CreateMap<Item2,Item2Dto>();

You can use AutoMapper's flattening feature to populate Item3Count. To do this, Item2Dto should have a property Item3ListCount and AutoMapper will translate this to Item3List.Count().

更多推荐

本文发布于:2023-07-27 08:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   实体   框架   属性   Entity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!