Path: redb/redb.Examples/ExamplesEf/E186_ObjectReference_ef.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/ExamplesEf/E186_ObjectReference_ef.cs ---- using System.Diagnostics; using Microsoft.EntityFrameworkCore; using redb.Core.Models.Entities; using redb.Examples.Ef; using redb.Examples.Ef.Entities; using redb.Examples.Examples; using redb.Examples.Output; using Biz = redb.Examples.Models; namespace redb.Examples.ExamplesEf; /// /// EF Core twin of : a project, an employee whose CurrentProject points at it, /// and the employee loaded whole with the referenced project and its header included. Both are deleted after. /// [ExampleMeta("E186_ef", "EF: Object Reference - Load", "Core", ExampleTier.Free, 1, "EF", "Reference")] public class E186_ObjectReference_ef : EfExampleBase { public override async Task RunEfAsync(IDbContextFactory contexts) { var sw = Stopwatch.StartNew(); var now = DateTime.UtcNow; var project = new objects { _id_scheme = EfSchemeIds.ProjectMetrics, _date_create = now, _date_modify = now, _name = $"RefProject_{now.Ticks}", ProjectMetricsProps = new ProjectMetricsProps { ProjectId = 1001, TasksCompleted = 50, TasksTotal = 100, Budget = 500000 } }; await using (var db = await contexts.CreateDbContextAsync()) { db.objects.Add(project); await db.SaveChangesAsync(); } var employee = EmployeeEfMapping.ToEntity(new RedbObject { name = $"RefEmployee_{now.Ticks}", Props = new Biz.EmployeeProps { FirstName = "John", LastName = "ProjectManager", Age = 35, Salary = 90000m, Department = "Engineering" } }); employee.EmployeeProps!.CurrentProject = project._id; await using (var db = await contexts.CreateDbContextAsync()) { db.objects.Add(employee); await db.SaveChangesAsync(); } EmployeeProps? loaded; await using (var db = await contexts.CreateDbContextAsync()) { loaded = await db.EmployeeProps.WithGraph() .Include(e => e.CurrentProjectNavigation!).ThenInclude(p => p.objects) .FirstOrDefaultAsync(e => e._id == employee._id); } sw.Stop(); await using (var db = await contexts.CreateDbContextAsync()) { await db.objects.Where(o => o._id == employee._id).ExecuteDeleteAsync(); await db.objects.Where(o => o._id == project._id).ExecuteDeleteAsync(); } if (loaded?.CurrentProjectNavigation == null) return Fail("E186_ef", "EF: Object Reference - Load", ExampleTier.Free, sw.ElapsedMilliseconds, "CurrentProject reference not loaded."); var business = EmployeeEfGraph.ToBusiness(loaded); return Ok("E186_ef", "EF: Object Reference - Load", ExampleTier.Free, sw.ElapsedMilliseconds, 1, [$"Employee: {business.Props.FirstName} {business.Props.LastName}", $"Project: {loaded.CurrentProjectNavigation.objects._name}", $"Project Budget: ${loaded.CurrentProjectNavigation.Budget ?? 0:N0}"]); } }