Wolfgang.DbContextBuilder.AutoFixture
0.9.0
Prefix Reserved
dotnet add package Wolfgang.DbContextBuilder.AutoFixture --version 0.9.0
NuGet\Install-Package Wolfgang.DbContextBuilder.AutoFixture -Version 0.9.0
<PackageReference Include="Wolfgang.DbContextBuilder.AutoFixture" Version="0.9.0" />
<PackageVersion Include="Wolfgang.DbContextBuilder.AutoFixture" Version="0.9.0" />
<PackageReference Include="Wolfgang.DbContextBuilder.AutoFixture" />
paket add Wolfgang.DbContextBuilder.AutoFixture --version 0.9.0
#r "nuget: Wolfgang.DbContextBuilder.AutoFixture, 0.9.0"
#:package Wolfgang.DbContextBuilder.AutoFixture@0.9.0
#addin nuget:?package=Wolfgang.DbContextBuilder.AutoFixture&version=0.9.0
#tool nuget:?package=Wolfgang.DbContextBuilder.AutoFixture&version=0.9.0
DbContextBuilder
Uses the Builder pattern to create Entity Framework Core and classic Entity Framework DbContext instances using an in-memory database for testing purposes. With DbContextBuilder, you can easily set up a DbContext with predefined data, making it ideal for unit tests and integration tests without having to rely on an actual database whose data can be changed or deleted over time.
๐ฆ Installation
DbContextBuilder ships as a family of packages โ install the one that matches your EF flavor:
| Package | EF flavor | Notes |
|---|---|---|
Wolfgang.DbContextBuilder-Core-EF6 |
EF Core 6 | Pins EF Core 6 for projects on the LTS 6.x line. |
Wolfgang.DbContextBuilder-Core-EF7 |
EF Core 7 | Pins EF Core 7. |
Wolfgang.DbContextBuilder-Core-EF8 |
EF Core 8 | Pins EF Core 8 (LTS). |
Wolfgang.DbContextBuilder-Core-EF9 |
EF Core 9 | Pins EF Core 9. |
Wolfgang.DbContextBuilder-Core-EF10 |
EF Core 10 | Pins EF Core 10. |
Wolfgang.DbContextBuilder-EF6 |
Classic EF 6 (non-Core) | For applications still on System.Data.Entity / EF 6.x. |
Wolfgang.DbContextBuilder-Coreis retired. Versions up to 0.8.1 remain on NuGet and keep resolving, but no new versions are published. It paired net10.0 with EF Core 6, which suited nobody: if you are on EF Core 6 move toWolfgang.DbContextBuilder-Core-EF6, and if you are on net10.0 move toWolfgang.DbContextBuilder-Core-EF10. The types and namespaces are unchanged, so the migration is a package-reference swap.
Random-data and shared add-on packages (install alongside your EF Core package):
| Package | Role |
|---|---|
Wolfgang.DbContextBuilder.AutoFixture |
AutoFixture-backed random data. Adds .UseAutoFixture() for SeedWithRandom. |
Wolfgang.DbContextBuilder.Bogus |
Bogus-backed random data (realistic fake values). Adds .UseBogus(). |
Wolfgang.DbContextBuilder.Abstractions |
Shared ICreateRandomEntities abstraction, EF-Core-version-independent. Referenced transitively by the provider packages. |
# Pick whichever matches your project's EF version, plus a random-data provider
dotnet add package Wolfgang.DbContextBuilder-Core-EF8
dotnet add package Wolfgang.DbContextBuilder.AutoFixture # or .Bogus
๐ฏ Supported Frameworks
Each package targets the .NET runtimes its EF version requires:
| Package | Target Frameworks |
|---|---|
Wolfgang.DbContextBuilder-Core-EF6 |
net6.0 |
Wolfgang.DbContextBuilder-Core-EF7 |
net7.0 |
Wolfgang.DbContextBuilder-Core-EF8 |
net8.0 |
Wolfgang.DbContextBuilder-Core-EF9 |
net9.0 |
Wolfgang.DbContextBuilder-Core-EF10 |
net10.0 |
Wolfgang.DbContextBuilder-EF6 (classic) |
net462; net47; net471; net472; net48; net481 |
Wolfgang.DbContextBuilder.Abstractions |
netstandard2.0; net10.0 |
Wolfgang.DbContextBuilder.AutoFixture |
net6.0; net7.0; net8.0; net9.0; net10.0 |
Wolfgang.DbContextBuilder.Bogus |
net6.0; net7.0; net8.0; net9.0; net10.0 |
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
๐ Documentation
- GitHub Repository: https://github.com/Chris-Wolfgang/DbContextBuilder
- API Documentation: https://Chris-Wolfgang.github.io/DbContextBuilder/
- CHANGELOG: CHANGELOG.md
- Contributing Guide: CONTRIBUTING.md
- DocFX Version Picker Troubleshooting: docs/DOCFX-VERSION-PICKER.md
โจ Features
In-memory
DbContextfor tests. By default, DbContextBuilder uses the EF Core InMemory provider. Pass your ownDbContextOptionsBuilderto switch to SQLite in-memory or any other provider.Seed with your own data using
.SeedWith<T>(...)โ accepts anIEnumerable<T>or aparams T[].Seed with random data using
.SeedWithRandom<T>(count)to simulate real-world databases where additional rows beyond your test fixtures exist. Choose a random-data provider โ.UseAutoFixture()(theWolfgang.DbContextBuilder.AutoFixturepackage) or.UseBogus()(Wolfgang.DbContextBuilder.Bogus) โ or plug in your ownICreateRandomEntitiesvia.UseCustomRandomEntityCreator(...).Composable. Chain
SeedWith,SeedWithRandom, and provider options in any order, then call.BuildAsync()to materialize theDbContext.
๐ Usage
// Create a DbContext with seeded random data and your test data
var context = await new DbContextBuilder<YourDbContext>()
// Pick a random-data provider (or .UseBogus()) so SeedWithRandom has a generator
.UseAutoFixture()
// Seed with 10 random entities
.SeedWithRandom<YourEntity>(10)
// Seed with specific data
.SeedWith
(
new YourEntity
{
Id = 1,
Name = "Test Entity"
}
)
// Seed with 5 more random entities
.SeedWithRandom<YourEntity>(5)
// Build the DbContext instance
.BuildAsync();
// Use the context in your tests
var sut = new YourService(context);
๐ API surface
The Core package exposes a small, focused surface. The full reference is on the docs site; the most-used entry points are:
| Type / Method | What it does |
|---|---|
DbContextBuilder<T> |
The builder itself. Construct one per test, chain configuration, then call BuildAsync(). |
.UseInMemory() |
EF Core InMemory provider. Fastest, ignores relational constraints. |
.UseSqlite() |
SQLite in-memory provider. Enforces relational constraints. |
.UseSqliteForMsSqlServer() |
SQLite in-memory with a SQL-Server compatibility layer that flattens schema-qualified table names and rewrites SQL-Server-specific default-value SQL. |
.UseAutoFixture() |
Plug AutoFixture in as the random-entity generator (used by SeedWithRandom). Requires the Wolfgang.DbContextBuilder.AutoFixture package. |
.UseBogus() |
Plug Bogus in as the random-entity generator (realistic fake values). Requires the Wolfgang.DbContextBuilder.Bogus package. |
.UseCustomRandomEntityCreator(creator) |
Plug in any ICreateRandomEntities implementation. |
.UseDbContextOptionsBuilder(opts) |
Bring your own DbContextOptionsBuilder<T> to override the provider entirely. |
.UseSeedProfile(profile) |
Apply a reusable ISeedProfile<T> โ a named bundle of seed data shareable across tests. Multiple profiles accumulate. |
.UseDiagnosticOutput(writeLine) |
Route EF Core logs (and a one-line seed summary) to a sink such as testOutputHelper.WriteLine. |
.SeedWith<TEntity>(...) |
Seed specific rows. Accepts IEnumerable<T> or params T[]. |
.SeedWithRandom<TEntity>(count, [func]) |
Seed N random rows (requires a random-data provider). Optional func mutates each generated entity. |
.BuildAsync() |
Materialize the DbContext. The builder owns the underlying connection; dispose the context with await using. |
SqliteModelCustomizer |
Customization hooks for the SQLite-for-SQL-Server mode: OverrideTableRenaming, OverrideDefaultValueHandling, OverrideComputedValueHandling, OverrideManyToManyTableHandling, DefaultValueMap. |
ICreateDbContext / ICreateRandomEntities |
Extension points for plugging in your own provider or random-entity generator. |
๐ ๏ธ Building from Source
Prerequisites
- .NET SDK - the current release (10.0); see Supported Frameworks for the targets that are built
- PowerShell 7 (
pwsh) for the scripts underscripts/
Build Steps
# Clone the repository
git clone https://github.com/Chris-Wolfgang/DbContextBuilder.git
cd DbContextBuilder
# Restore dependencies
dotnet restore
# Build the solution
dotnet build --configuration Release
# Run tests
dotnet test --configuration Release
# Run code formatting
pwsh ./scripts/format.ps1
# Run the PR workflow's Windows stage locally (build, tests on every TFM, coverage gates, DevSkim, gitleaks)
pwsh ./scripts/build-pr.ps1
๐ Verify the build
Every release publishes a signed SLSA build-provenance attestation, a CycloneDX SBOM, and a reproducible-build manifest alongside the NuGet packages. See SECURITY.md ยง Verifying the supply chain to verify a downloaded package came from this repository's CI, and docs/REPRODUCIBLE-BUILD.md to independently rebuild a release from source and compare hashes.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AutoFixture (>= 4.18.1)
- Wolfgang.DbContextBuilder.Abstractions (>= 0.9.0)
- Wolfgang.DbContextBuilder-Core-EF10 (>= 0.9.0)
-
net6.0
- AutoFixture (>= 4.18.1)
- Wolfgang.DbContextBuilder.Abstractions (>= 0.9.0)
- Wolfgang.DbContextBuilder-Core-EF6 (>= 0.9.0)
-
net7.0
- AutoFixture (>= 4.18.1)
- Wolfgang.DbContextBuilder.Abstractions (>= 0.9.0)
- Wolfgang.DbContextBuilder-Core-EF7 (>= 0.9.0)
-
net8.0
- AutoFixture (>= 4.18.1)
- Wolfgang.DbContextBuilder.Abstractions (>= 0.9.0)
- Wolfgang.DbContextBuilder-Core-EF8 (>= 0.9.0)
-
net9.0
- AutoFixture (>= 4.18.1)
- Wolfgang.DbContextBuilder.Abstractions (>= 0.9.0)
- Wolfgang.DbContextBuilder-Core-EF9 (>= 0.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.