Using EF 8 Core.
I have a catalog system. It contains pages. Pages can have other pages as well as items. I need to load child pages recursively.
I have this
var page = await dbContext
.Set<CatalogPage>()
.Where(x => x.Id == eventParser.PageId)
.Include(c => c.Items).ThenInclude(x => x.FurnitureItems)
.Include(c => c.Pages).ThenInclude(x => x.Pages)
.FirstOrDefaultAsync();
But it only loads one level deep.
This can keep going till the property ParentPageId
is -1
(no parent), or in EF’s case the opposite as it needs to load inner.
Here is my entity:
public class CatalogPage
{
[Key]
public int Id { get; init; }
public string? Name { get; init; }
public string? Caption { get; init; }
public string? Layout { get; init; }
public int? RoleId { get; init; }
public int CatalogPageId { get; init; }
public int OrderId { get; init; }
public int IconId { get; init; }
public ICollection<CatalogPage> Pages { get; init; } = [];
public ICollection<CatalogItem> Items { get; init; } = [];
}
New contributor
user24419920 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.