在相同类型上填充多个集合

时间:2017-08-06 11:02:05

标签: c# entity-framework entity-framework-core

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

当前选择

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; }
}

1 个答案:

答案 0 :(得分:0)

<强>解决方案:

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));