在同一对象上填充多个集合 [英] Populating multiple collection on same object

查看:85
本文介绍了在同一对象上填充多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要填充包含两个集合的Product对象。

当前代码工作正常并填充了Product.GraphicItems集合,但我还需要填充Product.InfoItems集合,但我无法弄清楚多个集合的语法。



当前选择



  var  result =  await   this  .Context.ShopCategorys 
。包括(cat = > cat.InfoItems)
.Include(cat = > cat.Products)
.ThenInclude(prd = > prd.GraphicItems)
.ThenInclude(itm = > itm.Graphic)
.ThenInclude(gfx = > gfx.Items)
.SingleAsync(cat = > cat.Id.Equals(id));







Product.cs



 [表(  ShopProduct)] 
public class 产品: BaseShop
{
public bool 活跃{获得; set ; } = true ;
public int CategoryId { get ; set ; }
public int CultureId { get ; set ; } = -1;
public 列表< ProductInfo> InfoItems { get ; set ; } = new 列表< ProductInfo>();
public 列表< ProductGraphic> GraphicItems { get ; set ; } = new 列表< ProductGraphic>();
}





ProductInfo.cs



 [表(  ShopProductInfo)] 
public class ProductInfo:BaseShop,IInfoItem
{
public int ? ProductId { get ; set ; }
public int CultureId { get ; set ; }
public 文化文化{ get ; set ; }
public string 名称{ get ; set ; }
public string 描述{ get ; set ; }
}





我尝试过:



不同.Include和.ThenInclude变体

解决方案

解决方案:



 var result = await this.Context.ShopCategorys 
.Include(cat => cat.InfoItems)
.Include(cat => cat.Products)
.ThenInclude (prd => prd.InfoItems)
.Include(cat => cat.Products)
.ThenInclude(prd => prd.GraphicItems)
.ThenInclude(itm => itm.Graphic)
.ThenInclude(gfx => gfx.Items)
.SingleAsync(cat => cat.Id.Equals(id));


I need to populate a Product object which contains two collections.
The current code works fine and populates the Product.GraphicItems collection, but I also need to populate the Product.InfoItems collection, but I can't figure out the syntax for multiple collections.

Current select:

var result = await this.Context.ShopCategorys
    	.Include(cat => cat.InfoItems)
    	.Include(cat => cat.Products)
    		.ThenInclude(prd => prd.GraphicItems)
    			.ThenInclude(itm => itm.Graphic)
    				.ThenInclude(gfx => gfx.Items)
    	.SingleAsync(cat => cat.Id.Equals(id));




Product.cs:

[Table("ShopProduct")]
    public class Product : BaseShop
    {
    	public bool Active { get; set; } = true;
    	public int CategoryId { get; set; }
    	public int CultureId { get; set; } = -1;
    	public List<ProductInfo> InfoItems { get; set; } = new List<ProductInfo>();
    	public List<ProductGraphic> GraphicItems { get; set; } = new List<ProductGraphic>();
    }



ProductInfo.cs:

[Table("ShopProductInfo")]
    public class ProductInfo : BaseShop, IInfoItem
    {
    	public int? ProductId { get; set; }
    	public int CultureId { get; set; }
    	public Culture Culture { get; set; }
    	public string Name { get; set; }
    	public string Description { get; set; }
    }



What I have tried:

Different .Include and .ThenInclude variations

解决方案

Solution:

var result = await this.Context.ShopCategorys
	.Include(cat => cat.InfoItems)
	.Include(cat => cat.Products)
		.ThenInclude(prd => prd.InfoItems)
	.Include(cat => cat.Products)
		.ThenInclude(prd => prd.GraphicItems)
			.ThenInclude(itm => itm.Graphic)
				.ThenInclude(gfx => gfx.Items)
	.SingleAsync(cat => cat.Id.Equals(id));


这篇关于在同一对象上填充多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆