Csag.Extensions.DataSeeding 2.0.0

Prefix Reserved
dotnet add package Csag.Extensions.DataSeeding --version 2.0.0
                    
NuGet\Install-Package Csag.Extensions.DataSeeding -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Csag.Extensions.DataSeeding" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Csag.Extensions.DataSeeding" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Csag.Extensions.DataSeeding" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Csag.Extensions.DataSeeding --version 2.0.0
                    
#r "nuget: Csag.Extensions.DataSeeding, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Csag.Extensions.DataSeeding@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Csag.Extensions.DataSeeding&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Csag.Extensions.DataSeeding&version=2.0.0
                    
Install as a Cake Tool

Csag.Extensions.DataSeeding

NuGet License: MIT

What's this about?

From the database seeding article in Wikipedia:

Database seeding is populating a database with an initial set of data. It's common to load seed data such as initial user accounts or dummy data upon initial setup of an application.

We've all been there - your data seeding code starts simple but grows into a monster file with thousands of lines and complex dependencies. This library helps you split that mess into manageable chunks with proper dependency handling.

Key Features

  • Attribute-Based Dependencies: Clean syntax using [DependsOn(typeof(...))] attributes
  • Multi-Dependency Support: Seeds can depend on multiple other seeds
  • Circular Dependency Detection: Catches circular dependencies with clear error messages
  • Scoped Service Support: Proper handling of scoped dependencies with lifetime management
  • Topological Sorting: Dependency resolution using Kahn's algorithm for execution order
  • Service Lifetime Safety: Prevents common DI issues like ObjectDisposedException

Quick Start

Install the package

dotnet add package Csag.Extensions.DataSeeding

The package targets net6.0 and net8.0.

Add to dependency injection

For Microsoft Dependency Injection:

services.AddDataSeeding();

This automatically scans the calling assembly for ISeed implementations and prepares them for ordered execution.

Advanced Usage

If your seeds are in a different assembly than the one calling the registration, use the explicit overload:

services.AddDataSeeding(typeof(Startup).Assembly);

Note: The parameterless overload is preferred for most scenarios. Only use the assembly parameter when seeds are located in a different project/assembly.

The ISeed interface

Create seeds by implementing the ISeed interface and using the [DependsOn] attribute to declare dependencies:

[DependsOn(typeof(TenantSeed), typeof(RoleSeed))]
public class UserSeed : ISeed
{
    public async Task SeedAsync()
    {
        // Your seeding logic here
    }
}

Seeds are instantiated by the dependency injection container, so you can inject services in the constructor, including scoped services.

Dependency Management

Defining Dependencies

Use the [DependsOn] attribute to specify dependencies. For multiple dependencies, list them in the attribute:

[DependsOn(typeof(TenantSeed), typeof(RoleSeed))]
public class UserSeed : ISeed
{
    public Task SeedAsync()
    {
        // Your seeding logic here
        return Task.CompletedTask;
    }
}
Single Dependency

For seeds with only one dependency:

[DependsOn(typeof(TenantSeed))]
public class UserSeed : ISeed
{
    public Task SeedAsync()
    {
        // Your seeding logic here
        return Task.CompletedTask;
    }
}
No Dependencies

Seeds without dependencies don't need the attribute:

public class TenantSeed : ISeed
{
    public Task SeedAsync()
    {
        // This seed has no dependencies
        return Task.CompletedTask;
    }
}

Scoped Service Injection

The library handles scoped services properly:

public class UserSeed : ISeed
{
    private readonly UserManager<User> userManager;
    private readonly ApplicationDbContext context;
    private readonly ILogger<UserSeed> logger;

    public UserSeed(
        UserManager<User> userManager, 
        ApplicationDbContext context,
        ILogger<UserSeed> logger)
    {
        this.userManager = userManager;
        this.context = context;
        this.logger = logger;
    }

    public async Task SeedAsync()
    {
        var user = new User { UserName = "admin" };
        await this.userManager.CreateAsync(user, "Password123!");
    }
}

Benefits:

  • Each seed gets a fresh DI scope
  • Scoped services are automatically disposed after execution
  • Prevents ObjectDisposedException and scope validation errors

The ISeeder interface

Resolve ISeeder from the service provider to execute seeds:

public class DataInitializer
{
    private readonly ISeeder seeder;

    public DataInitializer(ISeeder seeder)
    {
        this.seeder = seeder;
    }

    public async Task RunAsync()
    {
        await this.seeder.SeedAsync();
    }
}

Features

Circular Dependency Detection

The library detects circular dependencies and throws clear error messages:

// This will throw an InvalidOperationException
[DependsOn(typeof(SeedB))]
public class SeedA : ISeed 
{
    public Task SeedAsync() => Task.CompletedTask;
}

[DependsOn(typeof(SeedA))] // Circular!
public class SeedB : ISeed 
{
    public Task SeedAsync() => Task.CompletedTask;
}

Detects all types of cycles:

  • Simple cycles: A → B → A
  • Complex cycles: A → B → C → A
  • Multi-level cycles: A → B → C → D → A

Complex Dependencies

Seeds can have multiple dependencies:

[DependsOn(typeof(TenantSeed), typeof(RoleSeed), typeof(PermissionSeed))]
public class ComplexSeed : ISeed
{
    // Executes only after ALL dependencies complete
    public async Task SeedAsync() { /* ... */ }
}

Example Usage

// Base seed with no dependencies
public class UserRolesSeed : ISeed
{
    public Task SeedAsync()
    {
        // Seed user roles
        return Task.CompletedTask;
    }
}

// Single dependency
[DependsOn(typeof(UserRolesSeed))]
public class UsersSeed : ISeed
{
    public Task SeedAsync()
    {
        // Seed users (requires roles first)
        return Task.CompletedTask;
    }
}

// Multiple dependencies
[DependsOn(typeof(UsersSeed), typeof(PermissionsSeed))]
public class UserPermissionsSeed : ISeed
{
    public Task SeedAsync()
    {
        // Seed user permissions (requires both users and permissions)
        return Task.CompletedTask;
    }
}

Demo

Check out the demo project for a complete CMS seeding example with 13 interconnected seeds.

Product 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 was computed.  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 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0 53 11/22/2026