Guides

Parent/child chains and .Under

Typed parents, the anchor rule, ancestors, and jumping back with Under.

A child blueprint takes its parent

A class belongs to the school chosen for this branch. The class also needs the district ID for tenant-scoped queries. A child blueprint gets its school directly, and can walk the same branch to find the district.

Blueprints.cs
public static partial class Blueprints
{
    public static readonly ChildBlueprint<SchoolSeed, School, SchoolClass> Class =
        Blueprint.ChildOf<SchoolSeed, School, SchoolClass>(
            (school, ctx) =>
                new()
                {
                    Id = ctx.NewId("cls"),
                    // Keep both foreign keys on this school’s branch.
                    SchoolId = school.Id,
                    DistrictId = ctx.Ancestor<District>().Id,
                    Name = "Algebra I",
                    Capacity = 25,
                    CreatedAtUtc = ctx.Now,
                }
        );
}

The typed parent is the lambda's first argument. Reach further up with ctx.Ancestor<T>(), which walks the recorded parent links and throws if no ancestor of that type exists. When the school comes from the seed, use school.DistrictId as described in Seed rows.

Every Add re-anchors

The builder always has a current anchor: the seed, at the start, then the output of the latest Add. A child Add attaches to the anchor, so a chain reads top to bottom.

await TestBiome
    .Start(_db, out _)
    // Each child Add uses the parent created on the line above it.
    .AddDistrict(out _)
    .AddSchool(out _)
    .AddClass(out _)
    .AddStudent(out var student)
    .BuildAsync();
The anchor type lets the compiler check the next relationship. After AddSchool, IntelliSense offers AddClass. AddStudent becomes available once a class is the anchor.

.Under: siblings and other branches

A chain follows one branch at a time. To add a sibling or start another branch, return to a parent you already built with .Under(x). It adds no entities; the next Add attaches to x.

Siblings
await TestBiome
    .Start(out _)
    .AddDistrict(out _)
    .AddSchool(out var school)
    .AddClass(out _)
    // Return to the school so Geometry is a sibling of the first class.
    .Under(school)
    .AddClass(out _, c => c.Name = "Geometry")
    .AddStudent(out var student)
    .BuildAsync();

.Under accepts a single entity or an array. For example, .Under(schools).AddClasses(out _, 2) adds two classes to each school.

Branches
await TestBiome
    .Start(out _)
    .AddDistrict(out _)
    .AddSchool(out var school)
    .AddClass(out var algebra)
    .AddStudent(out _)
    // Start a second class under the same school.
    .Under(school)
    .AddClass(out _, c => c.Name = "Geometry")
    .AddStudent(out _)
    // Resume the algebra branch to add its second student.
    .Under(algebra)
    .AddStudent(out var secondAlgebraStudent)
    .BuildAsync();

.Under takes Add outputs and the seed rows you listed in SeedEntities (for example .Under(seed.School)). A hand-built object, or an untracked part of an aggregate, can't be a parent, and Biome throws ArgumentException naming the offending index.

Anchors and sibling blueprints

Teachers and classes both belong to a school. Keep the original schools in an array and jump back before adding the second branch. Each child Add then fans out over the same schools.

SchoolTests.cs
await TestBiome
    .Start(out _)
    .AddDistrict(out _)
    .AddSchools(out var schools, 2)
    .AddClasses(out _, 2)
    // Teachers belong to schools, so restore the school anchor.
    .Under(schools)
    .AddTeachers(out var teachers, 2)
    .BuildAsync();
Capture an entity with out var x when you intend to .Under(x) later. Keep the starting builder (var biome = TestBiome.Start(...)) when you also want biome.All<T>() and biome.Last<T>() to inspect entities a helper created.

Errors you may see

MessageCause
A singular child AddOne requires exactly one parentThe previous step produced several (or zero) parents. Use the plural Add or .Under(schools[0]).
No ancestor of type X exists…A blueprint walks up to X, but the chain skipped X.
Parent at index N … is not owned by this biome session.Under was given an object Biome didn't build or select as seed.
This biome session is faulted because an earlier Add failedAn earlier Add threw. Fix that error; the session cannot continue.
An Add that fails may have created only part of its step. Biome marks that session faulted instead of allowing more setup on an incomplete graph. Look for the first exception, fix its cause, and start a new session.