Skip to content

ADR: Entity Framework, explained

ADR: Entity Framework, explained

Status
accepted
Date
2026-09-03
Shipped as
1.0.0.43

A companion to ADR: The relational store, written for a developer meeting EF Core for the first time. That record says what was decided and why; this one walks the setup file by file and explains what each piece is actually doing.

It exists for the same reason ADR: Program.cs, explained does. Generated configuration is the easiest place in a project to carry things nobody can account for, and a data layer is the worst place for that to be true.

In plain words

This page walks through how the code talks to the database (Entity Framework Core), one file at a time, for a developer meeting it for the first time. Every generated file is explained, so nothing in the data layer is there without a reason someone can state.

What that is worth: a developer new to EF Core can change the schema safely after one read, and the organization's data layer carries no setup that nobody can account for.

The five pieces

A DbContext is a session with the database. YardDbContext is a class that names the tables it can see and, through DbSet<T>, gives you something you can write LINQ against. When you write db.Vehicles.Where(...), EF turns that expression into SQL and runs it. The context also tracks the objects it handed you so that changing one and calling SaveChanges() produces an UPDATE for exactly the columns you touched.

A context is not thread-safe and is not meant to be long-lived. It is a unit of work: open, do a thing, dispose.

IDbContextFactory<T> is how you get one when you are not in a request. The usual advice for a web application is AddDbContext, which gives every HTTP request its own context and disposes it at the end. That is the right default and it is not what this application needs. The catalogue is read once at startup, before any request exists, and the two sources and the bid store are singletons that outlive every request. AddDbContextFactory registers a factory instead, and each of them opens a context for the length of one operation and disposes it. using var db = factory.CreateDbContext(); is that in one line.

A migration is the schema, as code, with a history. Running dotnet ef migrations add InitialCreate compares the model in YardDbContext against the last known state and writes three files: the migration itself, with an Up that creates the tables and a Down that drops them; a .Designer.cs holding a snapshot of the model at that point; and an updated YardDbContextModelSnapshot.cs, which is what the next migration will compare against.

At startup, db.Database.Migrate() looks at a table called __EFMigrationsHistory, sees which migrations have already been applied to this particular database, and runs the ones that have not. That is the whole mechanism, and it is why the alternative was rejected: EnsureCreated() makes the tables and writes no history, so the second time the model changes there is nothing to compare against and no way forward except deleting the database.

The design-time factory keeps the tooling out of the application. When dotnet ef needs to know what the model looks like, it will by default start your application to find a context in its service provider. That would be fine in most projects and is actively harmful in this one: this application migrates and seeds during startup, so generating the very first migration would boot an app that tries to seed tables the migration has not created yet. YardDbContextFactory implements IDesignTimeDbContextFactory<YardDbContext>, which the tooling prefers over booting anything. It hands back a context with a throwaway connection string, because the tooling only ever reads the model from it.

There is a matching detail worth knowing because the error message is the only place it is stated clearly: Microsoft.EntityFrameworkCore.Design has to be referenced by the startup project, not only by the project holding the context. That is what dotnet ef migrations add failed on the first time it was run here.

Primitive collections are stored as JSON. VehicleRow.DamageNotes is a List<string>, and EF Core maps it to a single text column holding a JSON array. No join table, no second entity. That is the right shape for a list that is only ever read back whole with its owner, which these two are.

The three files with the same name

Look in the directory holding the database during a run and there are three files, not one:

file what it is
yard.db the database
yard.db-wal the write-ahead log
yard.db-shm shared memory, coordinating readers and the log

SQLite is in write-ahead logging mode. A committed transaction is written to the log and fsynced there, and moved into the database proper by a checkpoint later, usually at a clean shutdown. That is why a database file can be 4,096 bytes, exactly one page, while holding two hundred vehicles: they are in the log, and the next process to open the pair replays it.

Two practical consequences. Copying yard.db on its own can copy an empty database, so the log has to come with it. And a size check on the database file alone can report that the data vanished when it did not, which is exactly what happened here and is written up in ADR: The relational store.

Why the row types are not the domain records

TheYard.Data.Vehicle is a sealed record with required init properties and IReadOnlyList<string> collections. Every one of those choices is deliberate and every one of them is awkward for a persistence layer: init-only members cannot be set field by field after construction, and a read-only interface is not something EF can populate.

The alternative to a second type is loosening the first one, and that trade runs the wrong way. The domain record is used everywhere and its immutability is load-bearing: with copies drive the bid overlay, and value equality drives the tests. So VehicleRow is a plain mutable class, VehicleRows maps between them in one file, and the awkwardness is confined to a mapper instead of spread through the application.

The honest cost is in the other record: adding a field to a vehicle is now three edits rather than one.

What AsNoTracking is doing there

By default a context remembers every entity it returns so it can work out what changed later. That bookkeeping is worth paying for when you intend to write. The catalogue reads never write, so AsNoTracking() tells EF to skip it. On a few hundred rows the difference is small. It is in the code because the habit matters more than this instance: a read path that tracks is a read path that allocates and holds objects for no reason.

What the seed is checking, and why

YardSeed.EnsureSeeded asks whether the table is empty, not whether the file is new. Those come apart in exactly one case that matters: a process that died partway through its first seed leaves a database with a schema and no rows. The emptiness check fills it on the next start. A "have I seeded before" flag would have recorded the attempt and left the tables empty forever.

In the code

The context and its model (api/TheYard.Infrastructure/YardDbContext.cs):

/// <summary>
/// The model for both providers: Identity's tables with their keys
/// narrowed, the catalogue, the photos, the bids and the activity
/// counters, with the SQL Server only parts behind one flag.
/// </summary>
protected override void OnModelCreating(ModelBuilder model)
{
    // Identity's own tables first. Skipping this call is the classic way to
    // get a context that compiles, migrates, and has nowhere to put a user.
    base.OnModelCreating(model);

    // The two providers differ in what they can express, not in what the
    // application means. Everything below that is inside this flag is a
    // SQL Server feature SQLite has no equivalent for.
    bool sqlServer = Database.IsSqlServer();

    // Identity's key columns, narrowed from its default of 450. A clustered
    // index key is capped at 900 bytes on SQL Server and nvarchar(450) is
    // 900 bytes on its own, so Identity's own composite keys are over the
    // cap out of the box: at the default widths PK_AspNetUserTokens is
    // 2,700 bytes and PK_Bids is 1,028, each of which fails an insert on a
    // long enough value. The ids this
    // application creates are GUIDs. The widths here exist to match
    // api/TheYard.Database, which is the authority, and a test holds them
    // to it (ADR: Data first, and the database in source control).
    model.Entity<YardUser>().Property(user => user.Id).HasMaxLength(YardColumns.IdentityKeyLength);
    model.Entity<IdentityRole>().Property(role => role.Id).HasMaxLength(YardColumns.IdentityKeyLength);
    model.Entity<IdentityUserClaim<string>>().Property(claim => claim.UserId).HasMaxLength(YardColumns.IdentityKeyLength);
    model.Entity<IdentityRoleClaim<string>>().Property(claim => claim.RoleId).HasMaxLength(YardColumns.IdentityKeyLength);
    model.Entity<IdentityUserLogin<string>>(login =>
    {
        login.Property(row => row.LoginProvider).HasMaxLength(YardColumns.IdentityKeyLength);
        login.Property(row => row.ProviderKey).HasMaxLength(YardColumns.IdentityKeyLength);
        login.Property(row => row.UserId).HasMaxLength(YardColumns.IdentityKeyLength);
    });
    model.Entity<IdentityUserRole<string>>(role =>
    {
        role.Property(row => row.UserId).HasMaxLength(YardColumns.IdentityKeyLength);
        role.Property(row => row.RoleId).HasMaxLength(YardColumns.IdentityKeyLength);
    });
    model.Entity<IdentityUserToken<string>>(token =>
    {
        token.Property(row => row.UserId).HasMaxLength(YardColumns.IdentityKeyLength);
        token.Property(row => row.LoginProvider).HasMaxLength(YardColumns.IdentityKeyLength);
        token.Property(row => row.Name).HasMaxLength(YardColumns.IdentityKeyLength);
    });

    model.Entity<VehicleRow>(vehicle =>
    {
        vehicle.ToTable("Vehicles");

        // Natural keys throughout. A vehicle already has an id the rest of
        // the system uses, a photo already has a unique file name, and a
        // bid is one per buyer per vehicle by definition, so a surrogate
        // key here would be a second identity to keep in step with the
        // first.
        vehicle.HasKey(row => row.Id);

        // Insertion order, kept explicitly. The synthetic scale-up expands
        // the seed catalogue deterministically from its order, so a set
        // that came back in a different order would be a different hundred
        // thousand vehicles. A table has no order of its own; this column
        // is what makes the answer the same every time.
        vehicle.Property(row => row.Seq).IsRequired();
        vehicle.HasIndex(row => row.Seq).IsUnique();

        // Which index is clustered is not decided here. On SQL Server the
        // physical design belongs to the SQL project, which puts the
        // clustered index on Seq because that is the only column this table
        // is ever read in order of, and leaves the primary key
        // nonclustered. This model maps to that schema and does not build it
        // (ADR: Data first, and the database in source control).

        vehicle.Property(row => row.Id).HasMaxLength(YardColumns.IdLength);

        // Seventeen characters, from a defined alphabet, by ISO 3779. This
        // is the one string in the catalogue whose length and character set
        // are fixed by a standard rather than by whatever the dataset
        // happens to hold, so it is the one that gets varchar rather than
        // nvarchar. Not fixed-length: nchar pads on read, and a padded
        // string would stop matching the record it came from.
        vehicle.Property(row => row.Vin).HasMaxLength(17).IsUnicode(false);

        vehicle.Property(row => row.Make).HasMaxLength(64);
        vehicle.Property(row => row.Model).HasMaxLength(64);
        vehicle.Property(row => row.Trim).HasMaxLength(64);
        vehicle.Property(row => row.BodyStyle).HasMaxLength(32);
        vehicle.Property(row => row.ExteriorColor).HasMaxLength(32);
        vehicle.Property(row => row.InteriorColor).HasMaxLength(32);
        vehicle.Property(row => row.Engine).HasMaxLength(128);
        vehicle.Property(row => row.Transmission).HasMaxLength(64);
        vehicle.Property(row => row.Drivetrain).HasMaxLength(16);
        vehicle.Property(row => row.FuelType).HasMaxLength(32);
        vehicle.Property(row => row.ConditionReport).HasMaxLength(1024);
        vehicle.Property(row => row.TitleStatus).HasMaxLength(32);
        vehicle.Property(row => row.Province).HasMaxLength(64);
        vehicle.Property(row => row.City).HasMaxLength(64);
        vehicle.Property(row => row.SellingDealership).HasMaxLength(128);
        vehicle.Property(row => row.Lot).HasMaxLength(32);

        if (sqlServer)
        {
            // A grade between 1.0 and 5.0 to one decimal place. `float` can
            // represent 2.7 only approximately, and this number is compared
            // and displayed rather than accumulated, so the exact type is
            // the right one. The CLR property stays `double` because that is
            // what the domain record uses and the persistence layer does not
            // get to change the domain's shape.
            vehicle.Property(row => row.ConditionGrade).HasConversion<decimal>().HasPrecision(3, 1);

            // An instant, stored as an instant. The dataset carries it as
            // `2026-04-05T19:00:00`, which is a local wall-clock time with no
            // zone, so `datetime2(0)` is exactly the type for it: seconds
            // precision, no offset, and sortable and comparable in the
            // database rather than only in C#. The property stays a string
            // for the same reason the grade stays a double.
            vehicle.Property(row => row.AuctionStart)
                .HasConversion(YardColumns.AuctionStartToDateTime)
                .HasColumnType("datetime2(0)");
        }

        // DamageNotes and Images are primitive collections: EF stores each
        // as a JSON array in one column. They are read and written whole and
        // never queried into, which is the case a JSON column is for. The
        // day something filters on a damage note is the day this becomes a
        // table, and not before (ADR: The SQL Server backend).
    });

    model.Entity<PhotoRow>(photo =>
    {
        photo.ToTable("Photos");
        photo.HasKey(row => row.File);
        photo.Property(row => row.Seq).IsRequired();
        photo.HasIndex(row => row.Seq).IsUnique();

        photo.Property(row => row.File).HasMaxLength(128);
        photo.Property(row => row.Style).HasMaxLength(32);
        photo.Property(row => row.Title).HasMaxLength(256);
    });

    model.Entity<BidRow>(bid =>
    {
        bid.ToTable("Bids");

        // A bid belongs to a person and to a vehicle, and one person has one
        // standing bid per vehicle, so the pair is the key
        // (ADR: Accounts and per-user bids).
        bid.HasKey(row => new { row.UserId, row.VehicleId });

        // Identity's key column is nvarchar(450) on SQL Server, and a
        // foreign key has to match the column it points at.
        bid.Property(row => row.UserId).HasMaxLength(YardColumns.IdentityKeyLength);
        bid.Property(row => row.VehicleId).HasMaxLength(YardColumns.IdLength);

        // A real foreign key, and the only one this model can honestly
        // carry. Deleting an account takes its bids with it, which is the
        // right answer and is the database's answer rather than something
        // the application has to remember to do.
        bid.HasOne<YardUser>()
            .WithMany()
            .HasForeignKey(row => row.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        // There is deliberately no foreign key from VehicleId to Vehicles.
        // The catalogue in this table is 200 rows expanded in memory to
        // 100,000 by SyntheticVehicleSource, and a visitor bids on the
        // expanded set, so 99.8 per cent of legitimate bids name a vehicle
        // id that has no row here. A constraint would reject them. The
        // constraint becomes correct the day the expansion is persisted,
        // and writing it before then would be writing a rule the
        // application breaks on purpose (ADR: The SQL Server backend).

        // No index on UserId either, and its absence is the point: the
        // primary key is (UserId, VehicleId), so its leading column already
        // answers every "what has this person bid on" query. An index on
        // UserId would be a second copy of the first half of the key,
        // costing a write on every bid and earning nothing.

        // The concurrency token. Two containers, or two requests that get
        // past one container's lock, can both read this row and both decide
        // to write it; the token is what makes the second write fail
        // instead of silently overwriting the first.
        var version = bid.Property(row => row.RowVersion);
        if (sqlServer)
        {
            // SQL Server keeps the token itself: `rowversion` is a database
            // generated, monotonically increasing 8-byte value, and nothing
            // in the application can forget to bump it.
            version.IsRowVersion();
        }
        else
        {
            // SQLite has no rowversion. The token still exists and still
            // fails a stale write; the difference is that the store assigns
            // it, which is why EfBidStore sets one on every save.
            version.IsConcurrencyToken();
        }
    });

    ActivityModel.Configure(model);
    CostModel.Configure(model);
    StandingsModel.ConfigureStandings(model);
    CatalogueModel.ConfigureCatalogue(model, sqlServer);
    BlogModel.ConfigureBlog(model, sqlServer);
    LinkedInModel.ConfigureLinkedIn(model, sqlServer);
    ForemanModel.Configure(model, sqlServer);
    CatalogueModel.ConfigureCatalogueWords(model);
}

Live from api/TheYard.Infrastructure/YardDbContext.cs, region model, read from this build at 99df7a8.

The seed (api/TheYard.Infrastructure/Shared/Auction/EfSources.cs):

/// <summary>
/// Fills the vehicle table and the photo table, each only when it is empty,
/// in one save, and returns what was inserted and what each table now holds.
/// </summary>
/// <param name="db">The context to seed on.</param>
/// <param name="vehicles">Where the seed vehicles come from.</param>
/// <param name="photos">Where the seed photo manifest comes from.</param>
/// <param name="cancellation">The host's; this runs once while the container is coming up.</param>
public static async Task<SeedResult> EnsureSeededAsync(YardDbContext db, IVehicleSource vehicles, IPhotoManifestSource photos, CancellationToken cancellation)
{
    int vehiclesAdded = 0;
    int photosAdded = 0;

    // "Empty" rather than "new", so a database that half-filled because a
    // process died mid-seed is not left half-filled forever.
    if (!await db.Vehicles.AnyAsync(cancellation))
    {
        var rows = (await vehicles.LoadAsync(cancellation)).Select((vehicle, index) => vehicle.ToRow(index)).ToList();
        db.Vehicles.AddRange(rows);
        vehiclesAdded = rows.Count;
    }

    if (!await db.Photos.AnyAsync(cancellation))
    {
        var rows = (await photos.LoadAsync(cancellation))
            .Select((photo, index) => new PhotoRow
            {
                Seq = index,
                File = photo.File,
                Style = photo.Style,
                Title = photo.Title,
            })
            .ToList();
        db.Photos.AddRange(rows);
        photosAdded = rows.Count;
    }

    if (vehiclesAdded > 0 || photosAdded > 0)
    {
        await db.SaveChangesAsync(cancellation);
    }

    return new SeedResult(vehiclesAdded, photosAdded, await db.Vehicles.CountAsync(), await db.Photos.CountAsync());
}

Live from api/TheYard.Infrastructure/Shared/Stores/YardSeed.cs, region seed, read from this build at 99df7a8.

The bid store, which is the only thing here that writes (api/TheYard.Infrastructure/Shared/Auction/EfSources.cs):

/// <summary>Every stored bid, for BidBook to replay into its indexes at startup.</summary>
/// <param name="cancellation">The caller's; the replay's starter lends it, and in a container that is the host.</param>
public async Task<IReadOnlyList<StoredBid>> LoadAsync(CancellationToken cancellation)
{
    using var db = await factory.CreateDbContextAsync(cancellation);
    return await db.Bids.AsNoTracking().Select(row => new StoredBid(
            row.UserId,
            row.VehicleId,
            new BidState(row.Amount, row.BidCount, row.WonBuyNow, row.AtMs)))
        .ToListAsync(cancellation);
}

/// <summary>
/// Every buyer's row on one vehicle. There is no index on VehicleId alone,
/// so this reads the table; it runs only when a vehicle has no standing yet,
/// once per vehicle, on a table of visitor bids.
/// </summary>
/// <param name="vehicleId">The vehicle.</param>
/// <param name="cancellation">The caller's. A table read is the most expensive thing this adapter does, so it is the one most worth stopping.</param>
public async Task<IReadOnlyList<StoredBid>> BidsOnAsync(string vehicleId, CancellationToken cancellation)
{
    using var db = await factory.CreateDbContextAsync(cancellation);
    return await db.Bids.AsNoTracking().Where(row => row.VehicleId == vehicleId).Select(row => new StoredBid(
            row.UserId,
            row.VehicleId,
            new BidState(row.Amount, row.BidCount, row.WonBuyNow, row.AtMs)))
        .ToListAsync(cancellation);
}

/// <summary>
/// The buyer's row and the vehicle's standing in one transaction, the
/// standing first with a compare-and-set on its version. An update that
/// names a version no longer there changes no row, and an insert where
/// another writer has just inserted breaks the key; either way the
/// transaction is rolled back and the answer is false, so BidBook reads
/// again and runs the rules again. The buyer's row keeps its own token,
/// which catches one account writing from two sites at once, and that too
/// answers false rather than writing over what it never saw.
///
/// <para>The token this takes is never handed to the transaction, and that is the whole of
/// what the ports record promised: a write abandoned between the standing and the buyer's
/// row would leave the store holding a standing nothing has a row for. The caller above
/// has already stopped honouring cancellation by the time it gets here, so the parameter
/// exists to keep one shape across the port and not to be used (ADR: The ports learn to
/// wait, the addendum on the token).</para>
/// </summary>
public async Task<bool> TrySaveAsync(string userId, string vehicleId, BidState state, VehicleStanding standing, string? expectedVersion, CancellationToken cancellation)
{
    using var db = await factory.CreateDbContextAsync();
    await using var transaction = await db.Database.BeginTransactionAsync();
    if (expectedVersion is not null && !await TryMoveStandingAsync(db, vehicleId, standing, long.Parse(expectedVersion, CultureInfo.InvariantCulture), CancellationToken.None))
    {
        return false;
    }

    if (expectedVersion is null)
    {
        db.Standings.Add(RowOf(vehicleId, standing, version: 1));
    }

    await StageBuyerRowAsync(db, userId, vehicleId, state, cancellation);
    try
    {
        await db.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        return false;
    }
    catch (DbUpdateException) when (expectedVersion is null)
    {
        // An insert that broke a key lost to another writer's insert of the
        // same standing. Anything else that broke is a real failure and
        // travels.
        await transaction.RollbackAsync();
        if (await StandingExistsAsync(vehicleId, CancellationToken.None))
        {
            return false;
        }
        throw;
    }

    await transaction.CommitAsync();
    return true;
}

/// <summary>
/// The buyer's row for this vehicle, added or changed in place on the
/// context, with the concurrency token moved where the provider does not
/// move it itself. Nothing is written until the caller saves.
/// </summary>
private static async Task StageBuyerRowAsync(YardDbContext db, string userId, string vehicleId, BidState state, CancellationToken cancellation)
{
    var existing = await db.Bids.FindAsync(userId, vehicleId);
    if (existing is null)
    {
        existing = new BidRow
        {
            UserId = userId,
            VehicleId = vehicleId,
            Amount = state.Amount,
            BidCount = state.BidCount,
            WonBuyNow = state.WonBuyNow,
            AtMs = state.AtMs,
        };
        db.Bids.Add(existing);
    }
    else
    {
        existing.Amount = state.Amount;
        existing.BidCount = state.BidCount;
        existing.WonBuyNow = state.WonBuyNow;
        existing.AtMs = state.AtMs;
    }

    // SQL Server keeps the token itself; SQLite has no rowversion type, so
    // the store is what moves it. Setting it here rather than in a trigger
    // keeps the difference between the two providers in one readable place
    // (ADR: The SQL Server backend).
    if (db.Database.IsSqlite())
    {
        existing.RowVersion = Guid.NewGuid().ToByteArray();
    }
}

/// <summary>
/// Deletes one buyer's rows, every vehicle, and the standing of each of
/// those vehicles, in one transaction. The standings go so the next bid on
/// each vehicle works its standing out again from whoever is left.
///
/// <para>Like the bid write, the token is not given to the transaction: the standings and
/// the rows go together or not at all, and a reset stopped between them would leave
/// vehicles with no standing and rows still claiming one.</para>
/// </summary>
public async Task ClearAsync(string userId, CancellationToken cancellation)
{
    using var db = await factory.CreateDbContextAsync();
    await using var transaction = await db.Database.BeginTransactionAsync();
    // One person's rows and nobody else's. A delete over the whole table,
    // on a site two strangers can be looking at, would let either of them
    // delete the other's bids (ADR: Reset is one person's start-over).
    var touched = await db.Bids.Where(bid => bid.UserId == userId).Select(bid => bid.VehicleId).ToListAsync();
    await db.Standings.Where(row => touched.Contains(row.VehicleId)).ExecuteDeleteAsync();
    await db.Bids.Where(bid => bid.UserId == userId).ExecuteDeleteAsync();
    await transaction.CommitAsync();
}

Live from api/TheYard.Infrastructure/Shared/Auction/EfSources.cs, region bid-store, read from this build at 99df7a8.

Startup: migrate, then seed, then serve (api/TheYard.Api/Program.cs):

// The schema and the contents, before anything is registered, because the
// answer decides what gets registered. Migrate rather than EnsureCreated: the
// schema's history is files in this repository, so a container starting against
// an older database brings it forward instead of finding a shape it half
// recognises. A fresh database gets its contents from the JSON readers, so
// `npm run data` regenerates the dataset and the seed cannot drift from it.
//
// Two stores, two of everything below: each store is brought up on its own,
// timed on its own, and stands behind its own catalogue, bids, room and
// accounts (ADR: One container, both stores). What a request gets is decided
// per request, further down; nothing here knows which one a visitor will pick.
var seedVehicles = new JsonFileVehicleSource(dataPath);
var seedPhotos = new JsonFilePhotoManifestSource(manifestPath);
var backendList = new List<Backend>();

// #region sql-backend
// The relational store, on SQL Server or SQLite. A factory rather than a
// scoped context: the two sources and the bid store are singletons that each
// want a context for one operation, and there is no request scope at startup
// when the catalogue is read. The interceptor puts every statement on the Admin
// tab; it is attached here so YardConnection stays a description of where the
// database is.
// How long the first of the five asks waits after a refusal (two seconds unless the
// configuration shortens it, which only a test that proves the fallback does).
var firstWait = StorePrepare.FirstWaitFrom(builder.Configuration);
var sqlStartup = new StartupTimings();
var sqlState = await sqlStartup.Time("prepare", stopping => StorePrepare.WithTriesAsync(() => YardDatabase.PrepareAsync(yard, seedVehicles, seedPhotos, stopping), firstWait), cancellation);
// The backend stands on the files until its store is attached: the same two
// ports answered out of the JSON, the null bid store, no accounts, with the
// synthetic scale-up still decorating the vehicle source (ADR: The relational
// store). Attached at once below when the store came up, or by the second
// chance when it comes up later (ADR: The relational store, the addendum on
// the second chance).
var sqlBackend = new Backend
{
    Key = StoreKeys.Relational,
    Name = yard.Describe(),
    Database = sqlState,
    Startup = sqlStartup,
    Inventory = new Stock(new SyntheticVehicleSource(seedVehicles, targetCount), seedPhotos),
    Bids = new BidBook(new MemoryBidStore()),
    Market = new Room(new Random(), builder.Configuration.GetValue("Market:GraceSeconds", Room.DefaultGraceSeconds)),
    Probe = _ => Task.FromResult(false),
};
// Identity's store over the accounts tables, keyed by the store, given a context from the
// request's scope. The type is the one AddEntityFrameworkStores would have registered for a
// user type with no roles; naming it here is what lets the other backend register a
// different one under the same interface. Registered whether or not the store came up: the
// Accounts slice asks the backend whether it is ready before it asks for the store
// (ADR: The onion cut into vertical slices, rule 4: the backend names no adapter).
builder.Services.AddKeyedScoped<IUserStore<YardUser>>(sqlBackend.Key, (services, _) =>
    new Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore<YardUser, YardDbContext, string>(
        services.GetRequiredService<YardDbContext>(),
        services.GetService<IdentityErrorDescriber>()));
// Everything that stands on the relational store, built from a state that
// says it came up. The loggers are attached the moment the host exists (the
// factory is read at call time, so an attach after startup gets them too).
StoreAttachment SqlAttachment(DatabaseState state)
{
    var sqlOptions = new DbContextOptionsBuilder<YardDbContext>();
    yard.Configure(sqlOptions);
    sqlOptions.AddInterceptors(new SqlLogInterceptor(sqlLog, currentRequest, TimeProvider.System));
    var contexts = new ContextFactory(sqlOptions.Options);
    // The same database without the interceptor, for the activity counters:
    // a batch every five seconds would fill the SQL log with the feature that
    // reads the SQL log, the observer effect the request ring designed out
    // (ADR: Site activity, and the line an address does not cross).
    var quietOptions = new DbContextOptionsBuilder<YardDbContext>();
    yard.Configure(quietOptions);
    var quietContexts = new ContextFactory(quietOptions.Options);
    if (loggers.Factory is not null)
    {
        contexts.Attach(loggers.Factory);
    }
    relationalContexts.Hold(contexts, quietContexts);
    return new StoreAttachment(
        state,
        new RelationalDiagnostics(contexts, yard.Describe(), () => loggers.Factory?.CreateLogger<RelationalDiagnostics>()),
        Catalogues.Sql(contexts, yard, targetCount, Catalogues.UpkeepFor(keepCatalogues, defaultStore, StoreKeys.Relational, servesBothSites)),
        new BidBook(new EfBidStore(contexts)),
        new EfActivityStore(quietContexts),
        token => YardDatabase.HasSeedAsync(contexts, token));
}
if (sqlState.Ready)
{
    sqlBackend.Attach(SqlAttachment(sqlState));
}
backendList.Add(sqlBackend);
// #endregion sql-backend

// #region cosmos-backend
// The document store, when there is an account to talk to. Same question, same
// answer shape, other store: are the containers there with the keys this code
// was written for, and is the seed in them. A refusal falls through to the
// file-backed catalogue exactly as a missing schema does on SQL Server.
CosmosStore? cosmos = null;
StoreSecondChance.Plan? cosmosSecondChance = null;
if (cosmosConfigured)
{
    cosmos = CosmosConnection.Connect(
        configuredCosmos!,
        builder.Configuration["Cosmos:Database"] ?? "theyard",
        builder.Configuration["Cosmos:ContainerPrefix"] ?? "",
        // "managed-identity" on the deployed container; anything else, which
        // is what a developer's machine and the test runner have, means the
        // signed-in Azure CLI session.
        builder.Configuration["Cosmos:Credential"] ?? "azure-cli",
        AzureIds.ClientId(builder.Configuration),
        storeLog);
    // Every operation is filed under the request that caused it, the first one included.
    cosmos.Recorder.CurrentRequest = currentRequest;
    var cosmosStartup = new StartupTimings();
    var cosmosState = await cosmosStartup.Time("prepare", stopping => StorePrepare.WithTriesAsync(() => cosmos.Starting.PrepareAsync(seedVehicles, seedPhotos, stopping), firstWait), cancellation);
    var store = cosmos;
    // The relational backend's shape: the files until the store is attached (ADR: A
    // second store on Cosmos DB, and what it costs).
    var cosmosBackend = new Backend
    {
        Key = StoreKeys.Document,
        Name = "Azure Cosmos DB",
        Database = cosmosState,
        Startup = cosmosStartup,
        Diagnostics = new DocumentDiagnostics(cosmos),
        Inventory = new Stock(new SyntheticVehicleSource(seedVehicles, targetCount), seedPhotos),
        Bids = new BidBook(new MemoryBidStore()),
        Market = new Room(new Random(), builder.Configuration.GetValue("Market:GraceSeconds", Room.DefaultGraceSeconds)),
        Probe = _ => Task.FromResult(false),
    };
    // One document per account and one per address, and none of Identity's seven tables
    // (ADR: Accounts on a document store), keyed by the store the way the relational one is.
    builder.Services.AddKeyedScoped<IUserStore<YardUser>>(cosmosBackend.Key, (_, _) => new CosmosUserStore(store, TimeProvider.System));
    StoreAttachment CosmosAttachment(DatabaseState state) => new(
        state,
        new DocumentDiagnostics(store),
        // The same three ports, answered out of the document store.
        Catalogues.Cosmos(store, targetCount, Catalogues.UpkeepFor(keepCatalogues, defaultStore, StoreKeys.Document, servesBothSites)),
        new BidBook(new CosmosBidStore(store)),
        new CosmosActivityStore(store),
        store.Starting.ProbeAsync);
    if (cosmosState.Ready)
    {
        cosmosBackend.Attach(CosmosAttachment(cosmosState));
    }
    backendList.Add(cosmosBackend);
    cosmosSecondChance = new StoreSecondChance.Plan(
        cosmosBackend,
        () => store.Starting.PrepareAsync(seedVehicles, seedPhotos, CancellationToken.None),
        async state =>
        {
            var attachment = CosmosAttachment(state);
            await attachment.Inventory.WarmAsync(CancellationToken.None);
            await attachment.Bids.LoadAsync(CancellationToken.None);
            cosmosBackend.Attach(attachment);
        });
}
// #endregion cosmos-backend

// Which one a request gets: its header, the name it arrived on, or the default (ADR: One
// web app for every .NET process). Peer:Site is unset where one process serves both sites.
var backends = new Backends(
    backendList,
    defaultStore,
    builder.Configuration["Peer:Site"],
    siteHosts);
builder.Services.AddSingleton(siteHosts);
builder.Services.AddSingleton(backends);
builder.Services.AddScoped<CurrentBackend>();
// Identity's stores want a context per request and the factory hands out contexts, so this
// adapts one to the other (one of the four scoped registrations). The factory is read at
// call time, so a store attached after startup serves the accounts too.
builder.Services.AddScoped(_ =>
    (relationalContexts.Contexts ?? throw new InvalidOperationException("the relational store is not attached"))
        .CreateDbContext());
// #region second-chance-wiring
// A store that refused at startup is asked again after it (ADR: The relational
// store, the addendum on the second chance): every thirty seconds for an hour,
// attached warm when it answers. Only the backends that are not ready are
// planned, so a site whose stores came up runs nothing here.
var secondChances = new List<StoreSecondChance.Plan>
{
    new(
        sqlBackend,
        () => YardDatabase.PrepareAsync(yard, seedVehicles, seedPhotos, CancellationToken.None),
        async state =>
        {
            var attachment = SqlAttachment(state);
            await attachment.Inventory.WarmAsync(CancellationToken.None);
            await attachment.Bids.LoadAsync(CancellationToken.None);
            sqlBackend.Attach(attachment);
        }),
};
if (cosmosSecondChance is not null)
{
    secondChances.Add(cosmosSecondChance);
}
builder.Services.AddHostedService(services => new StoreSecondChance(secondChances, services.GetRequiredService<ILogger<StoreSecondChance>>()));
// #endregion second-chance-wiring
// The partition key experiment needs the document store; without one it says so.
builder.Services.AddSingleton<IStoreExperiment>(
    cosmos is not null ? new PartitionExperiment(cosmos, TimeProvider.System) : NoStoreExperiment.Instance);

Live from api/TheYard.Api/Shared/Wiring/StoreRegistration.cs, region migrate-and-seed, read from this build at 99df7a8.

If you want to change the schema

These four steps were right when this record was written and are now the second half of the job rather than the whole of it. The schema is authored as SQL first (ADR: Data first, and the database in source control), Entity Framework maps to it, and a conformance test fails the build if the two disagree. The full list, in order, is in ADR: Two providers and a SQL project, explained. The short version:

  1. Change the DDL in api/TheYard.Database, with a length and a reason.
  2. Change the row class in Rows.cs, and the mapping in EfSources.cs if the field also exists on the domain record.
  3. Add the SQLite migration, so local development and CI get the column too: dotnet ef migrations add <Name> --project api/TheYard.Migrations.Sqlite --startup-project api/TheYard.Api --context YardDbContext -- --sqlite
  4. Read the generated Up method. It is ordinary C# and it is where the change becomes real on SQLite.
  5. Commit all three generated files. The snapshot is not optional; without it the next migration compares against the wrong model.
  6. Publish the SQL project with SqlPackage when it ships.

Two things in the original wording are now wrong and are worth naming rather than deleting without a note. The migrations project is api/TheYard.Migrations.Sqlite and not api/TheYard.Infrastructure, because two providers produce two models and EF Core keeps one snapshot per assembly. And "there is no step for applying it, the application does that on start" is true of SQLite only: on SQL Server the application holds db_datareader and db_datawriter and cannot create or alter a table at all.

Addendum, 2026-09-08: the store this record explains is one of two

Everything above explains Entity Framework, and Entity Framework is the mapper for the relational store only. The second store, Azure Cosmos DB, is reached through the Cosmos DB SDK directly, with no context, no migrations and no mapper beyond a class per document and a method that copies fields (ADR: A second store on Cosmos DB, and what it costs). If you are reading this to learn how the deployed site on theyard.stevenstout.biz keeps its data, it is the right record; if you are reading it to learn how the second container keeps the same data, the newcomer's record for that side is ADR: Cosmos DB, explained for someone who knows SQL Server, and the reason the two are different is in the parent record.

One thing in "If you want to change the schema" changed shape: the ports the adapters implement are asynchronous now (ADR: The ports learn to wait), so an adapter's Load() is LoadAsync() and the calls inside it are the ...Async forms Entity Framework has always had.

Where it sits

This walkthrough explains Infrastructure code, YardDbContext and YardSeed, along with the design-time factory that now lives in the host's DesignTime.cs and the SQLite history in its own project beside the onion. The principle it teaches is single responsibility: VehicleRow changes when storage changes, the Data record Vehicle changes when the domain does, and VehicleRows is the one file that maps between them. That separation is paid for as extra edits on every new field. A team willing to let EF shape its records directly would save those edits and accept a domain record with mutable members.

Files

Enforced by

Not machine-checked. Held by review.

Store

The Yard, home

Steven StoutStaff .NET engineerRemote

Production .NET that stays up. Built with AI, measured on every version.

Steven Stout

Hi, I'm Steve. I build the systems that make delivery boring in the best way, then coach people inside them.

  • 200+

    production releases, none took the platform down

    Conway Investment Research, 2019 to 2024. Sole engineer for the first three years on a live financial advisory platform.

    See how
  • 50%

    shorter release cycle

    Storee Construction, 2024 to 2026. Four ASP.NET applications onto one .NET 10 platform on Azure, with a four-person team I hired and mentored.

    See how

01Verified

Steven Stout

A real person, and the work is mine.

  • Identity

    Checked by LinkedIn, not by me.

    LinkedIn verified my government ID.

  • Running system

    You are looking at it.

    Version 1.0.5.273 is live. Nothing ships unless it passes the automated gates first.

That's who I am. Here's how I work.

02Strengths

Ten strengths, each with the proof beside it. The numbers match my resume and LinkedIn, and the live ones are read from this site as you look.

  • 01

    Keeps production alive

    200+ releases on a live financial platform, and none took it down. Sole engineer for the first three years.

    See the Conway work

  • 02

    Simplifies messy systems

    Four ASP.NET applications onto one .NET 10 platform on Azure, and a release cycle 50% shorter.

    See the Storee work

  • 03

    Marries dev and infrastructure

    Blue-green releases at Storee that flipped in about 5 seconds with nobody signed out, and Azure Front Door at two employers.

    See this site's cloud

  • 04

    Designs data that holds up

    White-label clients on one shared database, the isolation built into the data, and no cross-client issues.

    See the Conway work

  • 05

    Keeps AI-written code honest

    15 quality numbers that may only improve: 273 releases in a row with none worse.

    See the quality numbers

  • 06

    Resourceful

    $30,000+ cut from Azure and vendor spend at Conway. This site's Azure bill is public, resource by resource.

    See the bill

  • 07

    Builds people up

    Hired and mentored a four-person team. 13 colleagues recommend me by name.

    Read the recommendations

  • 08

    Takes ideas to production

    This site: designed, built and run solo, with 352 versions measured through one gate.

    How it was built

  • 09

    Writes clearly

    Posts on real engineering problems, in plain words.

    Read the blog

  • 10

    Owns decisions

    153 decision records, each with its tradeoff and the call I made.

    See the decision records

Built with AI. Gated so quality never slides.

Steven Stout’s showcase: a used-vehicle auction on .NET and React, built and run by one engineer, live on Azure, with its decisions written down.

No quality number has gotten worse at any release since 1.0.5.1 on 10 November: 273 releases in a row.

One engineer, an AI agent and a gate that checks 15 quality numbers on every release. 352 versions measured, with every result public, the worst days included.

82 releases in the last 7 days, each through the same gate.

Async methods with no CancellationToken, may only fall

11091.0.3.1391.0.5.2731.0.4.115, the ratchet
Every number, on the Quality page

TheYard, right nowLive

Answering in

unread

02505007501,000

server time at p95, the usual five minutes of the last day.

Azure this month

unread

the bill, by resource, read from Azure. The Admin tab shows every line.

Since the last roll

unread

 

rolled in place by the pipeline, behind one gate.

People who have visited

unread

30 days agotoday

Scanners, my own tools' and my own reads left out.

03Start here

04Where teams are heading

How I see engineering with AI, and the setup on this site that backs it.

AI-augmented teams · Architecture and ownership

AI writes more of the code. Someone still has to own what ships.

Teams are going to be heavily AI-augmented. That moves the senior job toward architecture and technical ownership: deciding what to build in pieces small enough to verify, and owning what ships.

  • Task tracking will get automated too. Judgment stays human: seeing a design fail at scale before it does.
  • On this site the AI's first design held the whole catalogue in memory. I caught it when the App Service plan hit 82 per cent and moved the work to the store.

What the AI wrote, what I decided

07Tech stack

  •    
  •    
  •    

Read from the build, never typed by hand

What TheYard runs on

What we use, where it runs, its AWS twin, the version as built, and the file that proves it.

Cloud patterns, not one vendor: every Azure piece in this stack has a named AWS twin. Mapped, not yet run on AWS.

  • .NET 1114 projects
  • React 19.3TypeScript 7.0
  • Azure9 Bicep files
  • 11GitHub Actions workflows
The whole stack, with the file that proves each line

The last release the gate stopped was 1.0.5.258 on 2 December, at its browser 1 of 2 job.

08How it is built

09How it is run

10Who and why

Next step

11What has to work, where is it today, and what does success look like?

Book 20 minutes opens an email to me with those three questions in it, and I answer with a time.

Book 20 minutes

Your email app should have opened with this message. If it did not, copy it and send it from any mailbox.

Or message me on LinkedIn