Csag.Extensions.DataSeeding
3.0.0
Prefix Reserved
dotnet add package Csag.Extensions.DataSeeding --version 3.0.0
NuGet\Install-Package Csag.Extensions.DataSeeding -Version 3.0.0
<PackageReference Include="Csag.Extensions.DataSeeding" Version="3.0.0" />
<PackageVersion Include="Csag.Extensions.DataSeeding" Version="3.0.0" />
<PackageReference Include="Csag.Extensions.DataSeeding" />
paket add Csag.Extensions.DataSeeding --version 3.0.0
#r "nuget: Csag.Extensions.DataSeeding, 3.0.0"
#:package Csag.Extensions.DataSeeding@3.0.0
#addin nuget:?package=Csag.Extensions.DataSeeding&version=3.0.0
#tool nuget:?package=Csag.Extensions.DataSeeding&version=3.0.0
Csag.Extensions.DataSeeding
Database seeding fills a database with an initial set of data, for example the first user accounts or test data.
Seeding code often starts small and becomes one large file, with many parts that must run in a specific order. This library lets you split that code into small classes, the seeds. Each seed tells which seeds must run before it, and the seeder runs all seeds in the correct order.
What the library does
- You write each part of your seeding code as a seed: one small class with one method.
- You declare the seeds that must run first with the
[DependsOn]attribute. The seeder finds the order. - The seeds get their services from dependency injection, also scoped services such as an Entity Framework
DbContext. - When the seeds have a circular dependency, the seeder throws an error before any seed runs.
- The run can be cancelled with a
CancellationToken.
Packages
| Package | Reference it from |
|---|---|
Csag.Extensions.DataSeeding |
The project that registers and runs the seeds, usually the application. |
Csag.Extensions.DataSeeding.Abstractions |
A project that only defines seeds, for example a domain or core library of your application. It holds ISeed, ISeeder and DependsOnAttribute, and it has no dependency on other packages. |
Csag.Extensions.DataSeeding depends on Csag.Extensions.DataSeeding.Abstractions, so NuGet installs both. All types are in the Csag.Extensions.DataSeeding namespace. Both packages target net8.0 and net10.0.
Get started
Install the package
dotnet add package Csag.Extensions.DataSeeding
Write a seed
A seed is a public class that implements ISeed:
public class RolesSeed : ISeed
{
public Task SeedAsync(CancellationToken cancellationToken)
{
// Add the roles here.
return Task.CompletedTask;
}
}
When a seed needs data from other seeds, give their types to the [DependsOn] attribute. The seeder runs them before this seed:
[DependsOn(typeof(TenantsSeed), typeof(RolesSeed))]
public class UsersSeed : ISeed
{
private readonly AppDbContext db;
public UsersSeed(AppDbContext db)
{
this.db = db;
}
public async Task SeedAsync(CancellationToken cancellationToken)
{
// Add the users here. The tenants and the roles exist at this point.
await this.db.SaveChangesAsync(cancellationToken);
}
}
A seed without the attribute has no dependencies. The dependency injection container creates the seeds, so a seed can get services through its constructor.
Register the seeds
Call AddDataSeeding with the assembly that contains your seeds:
services.AddDataSeeding(typeof(Program).Assembly);
The method registers each public class of the assembly that implements ISeed and is not abstract. It returns the service collection, so you can chain more calls.
When your seeds are in more than one assembly, give all of them in one call:
services.AddDataSeeding(typeof(Program).Assembly, typeof(TenantsSeed).Assembly);
When a seed class is registered two times, for example by AddDataSeeding and by a manual registration, the resolution of ISeeder throws an InvalidOperationException.
Run the seeds
Get ISeeder from dependency injection and call SeedAsync:
public class DataInitializer
{
private readonly ISeeder seeder;
public DataInitializer(ISeeder seeder)
{
this.seeder = seeder;
}
public Task RunAsync(CancellationToken cancellationToken)
{
return this.seeder.SeedAsync(cancellationToken);
}
}
The seeder runs the seeds one after the other. It writes the order of the seeds to the log at the Debug level.
How a run works
Order
Each seed runs after all registered seeds that it names in [DependsOn]. For seeds without a dependency between them, the order is not defined.
When the seeds have a circular dependency, for example SeedA depends on SeedB and SeedB depends on SeedA, SeedAsync throws an InvalidOperationException before any seed runs. The message starts with "Circular dependency detected among seeds" and names each seed that cannot run: the seeds of the cycle and the seeds that depend on them.
Scopes and lifetimes
A scope is the unit of dependency injection that controls the lifetime of a scoped service. Each call of SeedAsync creates one scope, and every seed of the run comes from that scope. Thus two seeds that use the same scoped service get the same instance, for example the same DbContext. The seeder disposes the scope at the end of the run, also when a seed fails.
The seeds are registered as transient services, so each run creates new instances of the seeds. A seed can use singleton, scoped and transient services.
The seeder also creates the seeds each time you get ISeeder, to find the registered seeds. Do not do work in the constructor of a seed. Do the work in SeedAsync.
Cancellation
Give a CancellationToken to SeedAsync. The seeder checks the token before it starts and before each seed, and it gives the token to each seed. When the token is cancelled, SeedAsync throws an OperationCanceledException, and the seeds that did not start do not run.
A seed that runs stops only when it uses the token. Give the token to the asynchronous methods that the seed calls.
When a seed fails
When a seed throws an exception, the run stops. The exception goes to the caller of SeedAsync unchanged, and the seeds after it do not run. The library does not undo the changes of the seeds that ran before.
When the seeds run again
The library keeps no record of earlier runs. It does not skip a seed because an earlier call of SeedAsync ran it, for example at the last start of the application. Thus each seed must give the same result when it runs again. Check for the data before you add it:
public class AdministratorRoleSeed : ISeed
{
private readonly AppDbContext db;
public AdministratorRoleSeed(AppDbContext db)
{
this.db = db;
}
public async Task SeedAsync(CancellationToken cancellationToken)
{
if (await this.db.Roles.AnyAsync(role => role.Name == "Administrator", cancellationToken))
{
return;
}
this.db.Roles.Add(new Role { Name = "Administrator" });
await this.db.SaveChangesAsync(cancellationToken);
}
}
Known limits
- A dependency that is not registered is ignored. When
[DependsOn]names a seed that is not registered, for example because its assembly is not given toAddDataSeeding, the seed runs without it. No error occurs. - Only public seed classes are found.
AddDataSeedingdoes not register a seed class that isinternal, and no message tells you. - One seeder state for each process. The library keeps the list of seeds and the service provider in one place that the whole process shares. Each
ISeederthat you get from a service provider replaces them for everyISeederin the process. Thus, when one process builds more than one service provider, for example in integration tests, a seeder can run the seeds of another service provider.
Demo
The demo project seeds a fictional content management system with 13 seeds that depend on each other.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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
- Csag.Extensions.DataSeeding.Abstractions (>= 3.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Scrutor (>= 7.0.0)
-
net8.0
- Csag.Extensions.DataSeeding.Abstractions (>= 3.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Scrutor (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.0 | 0 | 11/30/2026 |
| 3.0.0-rc.0 | 0 | 11/30/2026 |
| 2.0.0 | 189 | 11/22/2026 |