Parent/child chains and .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.
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();
var now = DateTimeOffset.UtcNow;
now = new DateTimeOffset(now.Ticks - now.Ticks % 10, TimeSpan.Zero);
var district = new District
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = "Riverside Unified",
TimeZoneId = "America/Chicago",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
var school = new School
{
Id = $"sch_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
Name = "School 1",
Code = "S1",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
// Each child needs the foreign keys for its branch.
var schoolClass = new SchoolClass
{
Id = $"cls_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
SchoolId = school.Id,
Name = "Algebra I",
Capacity = 25,
CreatedAtUtc = now,
};
var student = new Student
{
Id = $"stu_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
ClassId = schoolClass.Id,
Seat = 1,
FullName = "Student 1",
EnrolledAtUtc = now,
};
_db.AddRange(district, school, schoolClass, student);
await _db.SaveChangesAsync();
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.
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.
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.
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();
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
| Message | Cause |
|---|---|
A singular child AddOne requires exactly one parent | The 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 failed | An earlier Add threw. Fix that error; the session cannot continue. |