Data Seeding
Relevant source files
The following files were used as context for generating this wiki page:Data seeding in this project is the process of populating the database with initial, predefined data when the database is first created or migrated. This is handled by Entity Framework Core's model creation pipeline. The primary purpose is to ensure that essential data, such as a default administrator user, is available immediately after the application setup.
The seeding mechanism is centralized within the DatabaseSeeder class and is invoked during the DbContext's model configuration. This ensures that the seed data is part of the database migration history, making it consistent and repeatable across different environments.
Seeding Architecture and Flow
The data seeding process is triggered within the UserDBContext when Entity Framework Core builds the database model. It follows a simple, clear flow: the DbContext calls an extension method which contains the logic for adding data to specific entities.
This diagram illustrates the sequence of calls during the model creation process that leads to data being seeded.
Key Components
| Component | Description | Source File |
|---|---|---|
UserDBContext | The main Entity Framework DbContext for the application. It orchestrates model configuration and seeding. | Data/UserDBContext.cs |
DatabaseSeeder | A static class containing the Seed extension method for ModelBuilder, which defines the actual data to be inserted. | Seeders/DatabaseSeeder.cs |
Program.cs | The application's entry point, responsible for registering the UserDBContext with the dependency injection container. | Program.cs |
| Migration Files | Auto-generated files by EF Core that contain the SQL scripts for schema changes, including the INSERT statements for seeded data. | Migrations/ |
Implementation Details
DatabaseSeeder Class
The core seeding logic resides in the DatabaseSeeder.cs file. It defines a static extension method Seed for the ModelBuilder class. This method uses the HasData API to populate the User table.
// Seeders/DatabaseSeeder.cs
public static class DatabaseSeeder
{
public static void Seed(this ModelBuilder modelBuilder)
{
_ = modelBuilder.Entity<User>().HasData(
new User()
{
Id = 1,
Email = "chihtt123@gmail.com",
UserName = "chi",
Password = "123",
Role = true
}
);
}
}
Sources: Seeders/DatabaseSeeder.cs:7-22
UserDBContext Integration
The UserDBContext class invokes the seeding process from within its OnModelCreating method. This method is called by Entity Framework Core to configure the database model. After applying all entity configurations, it calls the modelBuilder.Seed() extension method.
// Data/UserDBContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// ... Apply all configurations
modelBuilder.ApplyConfiguration(new UserConfiguration());
// ... other configurations
// Apply any seed data if necessary
modelBuilder.Seed();
}
Sources: Data/UserDBContext.cs:15-29
The relationship between the DbContext, the seeder, and the model is shown below.
Seeded Data
Currently, the seeder is configured to insert a single record into the User table. This record represents a default administrative user.
| Field | Value | Data Type |
|---|---|---|
Id | 1 | int |
Email | "chihtt123@gmail.com" | string |
UserName | "chi" | string |
Password | "123" | string |
Role | true | bool |
Sources: Seeders/DatabaseSeeder.cs:13-20
Reflection in Database Migrations
The HasData call is captured by Entity Framework Core when creating a new migration. The generated migration files and the model snapshot contain the data, ensuring it is applied when the migration is run against a database.
The following snippet from a migration designer file shows how the seed data is persisted in code.
// Migrations/20250108150417_AddResetTokenToUser.Designer.cs
modelBuilder.Entity("API.Models.User", b =>
{
// ... property definitions
b.ToTable("User", (string)null);
b.HasData(
new
{
Id = 1,
Email = "chihtt123@gmail.com",
Password = "123",
Role = true,
UserName = "chi"
});
});
Sources: Migrations/20250108150417_AddResetTokenToUser.Designer.cs:202-215, Migrations/UserDBContextModelSnapshot.cs:231-244
This process ensures that anyone setting up the project and running the database migrations will have this default user created automatically, simplifying initial setup and testing.