Path: redb/redb.Examples/Ef/PersonEfMapping.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/Ef/PersonEfMapping.cs
----
using Microsoft.EntityFrameworkCore;
using redb.Core.Models.Entities;
using redb.Examples.Ef.Entities;
using Biz = redb.Examples.Models;
namespace redb.Examples.Ef;
///
/// Persons and list items of the examples through EF Core: PersonProps.Status points at a row of
/// list_items, PersonProps.Roles is a table of such pointers. Turned into the business class with
/// values, as redb hands them out.
///
internal static class PersonEfMapping
{
/// A new person header with its Props; keys come from the database.
public static objects ToEntity(string name, int age, string email, long? statusItemId, IReadOnlyList roleItemIds)
{
var now = DateTime.UtcNow;
return new objects
{
_id_scheme = EfSchemeIds.Person,
_id_owner = 0,
_id_who_change = 0,
_date_create = now,
_date_modify = now,
_name = name,
PersonProps = new PersonProps
{
Name = name,
Age = age,
Email = email,
Status = statusItemId,
PersonProps_Roles = roleItemIds.Select((id, i) => new PersonProps_Roles { _idx = i, _value = id }).ToList()
}
};
}
/// Every person with its header, status item and role items, as a query.
public static IQueryable Persons(this FlatContext db) => db.PersonProps
.Include(p => p.objects)
.Include(p => p.StatusNavigation)
.Include(p => p.PersonProps_Roles).ThenInclude(r => r._valueNavigation)
.AsSplitQuery();
/// The list item as redb's business value.
public static RedbListItem ToItem(list_items i) => new()
{
Id = i._id,
IdList = i._id_list,
Value = i._value ?? string.Empty,
Alias = i._alias,
IdObject = i._id_object
};
/// The business object of a person loaded through .
public static RedbObject ToBusiness(PersonProps p)
{
var h = p.objects;
return new RedbObject
{
id = h._id,
parent_id = h._id_parent,
scheme_id = h._id_scheme,
owner_id = h._id_owner,
who_change_id = h._id_who_change,
date_create = h._date_create,
date_modify = h._date_modify,
name = h._name,
Props = new Biz.PersonProps
{
Name = p.Name ?? string.Empty,
Age = p.Age ?? 0,
Email = p.Email ?? string.Empty,
Status = p.StatusNavigation is { } status ? ToItem(status) : null,
Roles = p.PersonProps_Roles.Count == 0
? null
: p.PersonProps_Roles.OrderBy(r => r._idx)
.Where(r => r._valueNavigation != null)
.Select(r => ToItem(r._valueNavigation!))
.ToList()
}
};
}
}