Skip to content

Database: Seeding ​

Introduction ​

Seeders fill the database with data, for development or for records every installation needs. They live in database/seeders. A class named DatabaseSeeder is the entry point: db:seed runs it, and it calls the other seeders.

Writing Seeders ​

A seeder extends DotBlaze.Seeder and implements Run. Models, the DB facade, and every other facade work inside it:

csharp
using App.Models;

namespace App.Database.Seeders;

public class DatabaseSeeder : Seeder
{
    public override async Task Run()
    {
        if (!await User.Where(u => u.Email == "test@example.com").Exists())
            await User.Create(new { Name = "Test User", Email = "test@example.com", Password = "password" });

        await Call<PostSeeder>();
    }
}

Checking before inserting, as above, keeps a seeder safe to run more than once.

Calling Additional Seeders ​

Call<TSeeder>() runs another seeder. To run several in order, pass their types to Call:

csharp
await Call<PostSeeder>();

await Call(typeof(UserSeeder), typeof(PostSeeder), typeof(CommentSeeder));

Using Model Factories ​

Factories create many records with fake data in one line. From the sample app:

csharp
public class PostSeeder : Seeder
{
    public override async Task Run()
    {
        var author = await User.Where(u => u.Email == "test@example.com").FirstOrFail();
        await Post.Factory().For(author).CreateMany(12);

        await User.Factory().Has(Post.Factory().Count(2)).CreateMany(3);   // three more authors with two posts each
    }
}

Using Services in Seeders ​

Seeders are created through dependency injection, so the constructor can ask for registered services:

csharp
public class CountrySeeder(IHttpClientFactory http) : Seeder
{
    public override async Task Run()
    {
        // ...
    }
}

Running Seeders ​

Run the DatabaseSeeder:

shell
dotblaze db:seed

Run a specific seeder by class name. If two seeders in different namespaces share a name, use the full name (App.Database.Seeders.PostSeeder):

shell
dotblaze db:seed --class=PostSeeder

Seed right after migrating:

shell
dotblaze migrate --seed
dotblaze migrate:fresh --seed

NOTE

With migrate --seed and migrate:fresh --seed, the option that picks a different seeder is --seeder=Name, not --class=Name. This is intentional, as in Laravel.

Seeders run in the same way inside tests, see Seeding in Tests.

Complete Example: Seed a Tutorial Post ​

Use the walkthrough's post schema, then add this separate seeder:

csharp
// database/seeders/TutorialPostSeeder.cs
using Blog.Models;
using DotBlaze;

namespace Blog.Database.Seeders;

public class TutorialPostSeeder : Seeder
{
    public override async Task Run()
    {
        var author = await User.Where(user => user.Email == "test@example.com").FirstOrFail();
        if (await Post.Where(post => post.UserId == author.Id && post.Title == "Tutorial post").Exists())
            return;
        await new Post
        {
            UserId = author.Id,
            Title = "Tutorial post",
            Body = "Seeded from a complete documentation example.",
        }.Save();
    }
}

From the application directory:

shell
dotblaze migrate
dotblaze db:seed
dotblaze db:seed --class=TutorialPostSeeder
dotblaze db:seed --class=TutorialPostSeeder

The first seed creates the template user; the named seeder creates one tutorial post. Sequentially repeating it leaves that post unchanged. This existence check is not a concurrency guarantee: run seeders in one deployment process, or enforce a unique database key when concurrent creation must be supported. Open /posts while signed in to see the result.