I’m trying to recursively include the inner pages of this entity:
<code>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; } = [];
}
</code>
<code>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; } = [];
}
</code>
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; } = [];
}
I’ve coded this, but it doesn’t include past one level deep:
<code>private static void Load(ICollection<CatalogPage> pages, DbContext db)
{
foreach (var p in pages)
{
db.Entry(p).Collection(x => x.Pages).Load();
if (p.Pages.Count < 1)
{
continue;
}
Load(p.Pages, db);
}
}
</code>
<code>private static void Load(ICollection<CatalogPage> pages, DbContext db)
{
foreach (var p in pages)
{
db.Entry(p).Collection(x => x.Pages).Load();
if (p.Pages.Count < 1)
{
continue;
}
Load(p.Pages, db);
}
}
</code>
private static void Load(ICollection<CatalogPage> pages, DbContext db)
{
foreach (var p in pages)
{
db.Entry(p).Collection(x => x.Pages).Load();
if (p.Pages.Count < 1)
{
continue;
}
Load(p.Pages, db);
}
}
Usage of the method:
<code>var parentlessPages = await dbContext.Set<CatalogPage>()
.Include(x => x.Pages)
.Include(x => x.Items).ThenInclude(x => x.FurnitureItems)
.Where(x => x.CatalogPageId == -1)
.ToListAsync();
Load(parentlessPages.SelectMany(x => x.Pages).ToList(), dbContext);
</code>
<code>var parentlessPages = await dbContext.Set<CatalogPage>()
.Include(x => x.Pages)
.Include(x => x.Items).ThenInclude(x => x.FurnitureItems)
.Where(x => x.CatalogPageId == -1)
.ToListAsync();
Load(parentlessPages.SelectMany(x => x.Pages).ToList(), dbContext);
</code>
var parentlessPages = await dbContext.Set<CatalogPage>()
.Include(x => x.Pages)
.Include(x => x.Items).ThenInclude(x => x.FurnitureItems)
.Where(x => x.CatalogPageId == -1)
.ToListAsync();
Load(parentlessPages.SelectMany(x => x.Pages).ToList(), dbContext);
What am I doing wrong?