Seeding
A developer tool for seeding Entity Framework Core databases with realistic fake data. Built on top of Bogus, 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.SeedingFeatures
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.CustomerIdis synced to matchOrder.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
SaveChangesbatch size to control memory pressureExtensible fake data rules — implement
IFakeDataSetup<T>to customize generated values per entity type; implementations are auto-discovered via assembly scanningCustom type rules — register global
Fakerrules for primitive types (e.g. always generate validGuidor a currency-formatteddecimal)
Core Types
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>
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
FakeData globallyUse 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 standaloneFakeData can also be used independently to generate test objects outside of seeding:
Full example
This will:
Seed 50
Customerrows, then 100ProductrowsSeed
Orderrows — 1–5 per customer (50–250 orders total)Seed
OrderLinerows — 1–10 per order, withOrderLine.CustomerIdautomatically synced to the parent order'sCustomerIdSkip
OutboxMessageentirelyFlush to the database every 500 rows
Configuration reference
EfFakeDataSeederConfig
EfFakeDataSeederConfigEfFakeDataSeederConfig.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
FakeDataConfigLocale
"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