Path: redb/redb.Examples/ExamplesEf/E118_PropsListItemField_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/E118_PropsListItemField_ef.cs
----
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using redb.Core.Models.Entities;
using redb.Examples.Ef;
using redb.Examples.Examples;
using redb.Examples.Output;
using Biz = redb.Examples.Models;
namespace redb.Examples.ExamplesEf;
///
/// EF Core twin of : a person whose Status points at the item "Active",
/// saved and loaded back whole with the item included.
///
[ExampleMeta("E118_ef", "EF: Props - ListItem Field", "List", ExampleTier.Free, 1, "EF", "List")]
public class E118_PropsListItemField_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
long personId;
await using (var db = await contexts.CreateDbContextAsync())
{
var list = await db.lists.FirstOrDefaultAsync(l => l._name == "ExampleStatuses");
if (list == null)
{
sw.Stop();
return Fail("E118_ef", "EF: Props - ListItem Field", ExampleTier.Free, sw.ElapsedMilliseconds,
"List 'ExampleStatuses' not found. Run E114_ef first.");
}
var items = await db.list_items.Where(i => i._id_list == list._id).ToListAsync();
var activeStatus = items.FirstOrDefault(i => i._value == "Active");
if (activeStatus == null)
{
sw.Stop();
return Fail("E118_ef", "EF: Props - ListItem Field", ExampleTier.Free, sw.ElapsedMilliseconds,
"Item 'Active' not found. Run E115_ef first.");
}
var person = PersonEfMapping.ToEntity("John Doe", 30, "john@example.com", activeStatus._id, []);
db.objects.Add(person);
await db.SaveChangesAsync();
personId = person._id;
}
RedbObject? loaded;
await using (var db = await contexts.CreateDbContextAsync())
{
var entity = await db.Persons().FirstOrDefaultAsync(p => p._id == personId);
loaded = entity == null ? null : PersonEfMapping.ToBusiness(entity);
}
sw.Stop();
var statusValue = loaded?.Props?.Status?.Value ?? "N/A";
return Ok("E118_ef", "EF: Props - ListItem Field", ExampleTier.Free, sw.ElapsedMilliseconds, 1,
[$"Person ID: {personId}", $"Status: {statusValue}"]);
}
}