Path: redb/redb.Examples/ExamplesEf/E001_SaveAsync_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/E001_SaveAsync_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 : one new employee, mapped by hand and saved.
[ExampleMeta("E001_ef", "EF: SaveAsync - Create Employee", "CRUD", ExampleTier.Free, 1, "EF", "Save")]
public class E001_SaveAsync_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
await using var db = await contexts.CreateDbContextAsync();
var employee = new RedbObject
{
name = "New Developer",
Props = new Biz.EmployeeProps
{
FirstName = "Alice",
LastName = "Johnson",
Age = 28,
Position = "Developer",
Department = "Engineering",
Salary = 85000m,
HireDate = DateTime.Today,
EmployeeCode = "EMP-NEW-001",
Skills = ["C#", "React", "SQL"]
}
};
var header = EmployeeEfMapping.ToEntity(employee);
db.objects.Add(header);
await db.SaveChangesAsync();
sw.Stop();
return Ok("E001_ef", "EF: SaveAsync - Create Employee", ExampleTier.Free, sw.ElapsedMilliseconds,
[$"Created employee ID: {header._id}", $"Name: {employee.Props.FirstName} {employee.Props.LastName}"]);
}
}