Path: redb/redb.Examples/ExamplesEf/EfExampleBase.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/EfExampleBase.cs
----
using Microsoft.EntityFrameworkCore;
using redb.Core;
using redb.Examples.Ef;
using redb.Examples.Examples;
using redb.Examples.Output;
namespace redb.Examples.ExamplesEf;
///
/// Base class of the EF Core twins of the examples. A twin carries
/// the id of its redb example with the suffix _ef, works on the flat database through EF Core only,
/// and returns the same result shape, so the runner prints both side by side.
///
public abstract class EfExampleBase : ExampleBase
{
///
/// Factory of contexts over the flat database, set by Program before any EF example runs. A twin
/// creates a new context for each unit of work, as EF recommends; the model is built once per process.
///
public static IDbContextFactory? Contexts { get; set; }
///
public sealed override Task RunAsync(IRedbService redb) =>
RunEfAsync(Contexts ?? throw new InvalidOperationException(
"The EF context factory is not set: EF examples run only from Program."));
/// Runs the example through EF Core.
public abstract Task RunEfAsync(IDbContextFactory contexts);
///
/// Result of a twin whose task EF Core cannot do out of the box. No raw SQL stands in for the missing
/// feature: the comparison table says so instead of showing a time.
///
protected static ExampleResult NotSupported(string id, string title, ExampleTier tier, string reason) => new()
{
Id = id,
Title = title,
Tier = tier,
Success = true,
NotSupported = true,
ElapsedMs = 0,
Output = [reason]
};
}