Seeding

A developer tool for seeding Entity Framework Core databases with realistic fake data. Built on top of Bogusarrow-up-right, it introspects your DbContext model, respects foreign key relationships, and fills tables in the correct dependency order — with full control over row counts and relationship cardinality.

Intended for use in local development environments, integration tests, and demo setups.


Installation

dotnet add package Dosaic.DevTools.Seeding

Features

  • Automatic entity discovery — reads all non-owned entity types from the EF Core model

  • Topological sort — seeds principal tables before dependent tables, honoring every foreign key

  • FK-aware generation — pulls existing principal keys from the database and assigns them to dependent rows

  • Relation count control — configure exactly how many dependents to create per principal (exact, range, or pick-list)

  • Transitive FK sync — when an entity has two foreign keys that share a common ancestor, the redundant FK column is automatically kept consistent (e.g. OrderLine.CustomerId is synced to match Order.CustomerId)

  • Per-type and global row counts — set a default count for all types and override individually

  • Ignore list — skip specific entity types entirely

  • Batched saves — configurable SaveChanges batch size to control memory pressure

  • Extensible fake data rules — implement IFakeDataSetup<T> to customize generated values per entity type; implementations are auto-discovered via assembly scanning

  • Custom type rules — register global Faker rules for primitive types (e.g. always generate valid Guid or a currency-formatted decimal)


Core Types

Type
Description

EfFakeDataSeeder

Executes the seeding operation against a DbContext

EfFakeDataSeederConfig

Fluent configuration builder for the seeder

FakeData

Wrapper around Bogus Faker<T> with auto-discovery of IFakeDataSetup<T> implementations

FakeDataConfig

Configuration for FakeData (locale, strict mode, global type rules)

IFakeDataSetup<T>

Interface for declaring per-entity Bogus rules


Usage

Basic seeding

Seed all entity types in the model with the default count (10 rows each):

Controlling row counts

Controlling relationship cardinality

Use WithRelationCount to declare how many dependent rows to create per principal:

Ignoring entity types

Configuring the batch size

By default SaveChangesAsync is called every 200 rows. Adjust to tune memory vs. I/O:

Custom fake data rules with IFakeDataSetup<T>

Implement IFakeDataSetup<T> to attach Bogus rules to a specific entity type. Implementations are discovered automatically from all loaded assemblies — no registration required.

Configuring FakeData globally

Use FakeData.ConfigureInstance once at application startup to customise locale, strict mode, or primitive type rules:

Pass a custom FakeData instance directly to the config when needed:

Using FakeData standalone

FakeData can also be used independently to generate test objects outside of seeding:


Full example

This will:

  1. Seed 50 Customer rows, then 100 Product rows

  2. Seed Order rows — 1–5 per customer (50–250 orders total)

  3. Seed OrderLine rows — 1–10 per order, with OrderLine.CustomerId automatically synced to the parent order's CustomerId

  4. Skip OutboxMessage entirely

  5. Flush to the database every 500 rows


Configuration reference

EfFakeDataSeederConfig

Method
Default
Description

EfFakeDataSeederConfig.For(context)

Creates a config for the given DbContext using FakeData.Instance

EfFakeDataSeederConfig.For(context, fakeData)

Creates a config with a custom FakeData instance

.WithDefaultCountPerEntityType(n)

10

Row count for entity types without an explicit override

.WithTotalCount<T>(n)

Row count for a specific entity type

.WithIgnore<T>()

Exclude an entity type from seeding

.WithRelationCount<T>(nav, count)

Exact number of dependents per principal

.WithRelationCount<T>(nav, min, max)

Dependents per principal drawn from [min, max]

.WithRelationCount<T>(nav, params int[])

Dependents per principal chosen from the given options

.WithBatchSize(n)

200

Number of entities added before each SaveChangesAsync call

FakeDataConfig

Property / Method
Default
Description

Locale

"en"

Bogus locale string (e.g. "de", "fr")

UseStrictMode

false

Enables Bogus strict mode — every property must have a rule

.AddTypeRule<T>(Func<Faker, T>)

Global rule applied to all fakers for the given CLR type

Last updated