How to Add Seed Data to the Database
How to Add Seed Data to the Database 관련
One more important step is to seed the database with initial data when the application starts. This seed data will populate the database, allowing you to test your API endpoints without manually adding data.
Let's add some seed data before performing migrations and testing our API endpoints.
To achieve this, we will create a new class in our Configuration folder called BookTypeConfigurations
and add the following code:
using bookapi_minimal.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace bookapi_minimal.Configurations
{
public class BookTypeConfigurations : IEntityTypeConfiguration<BookModel>
{
public void Configure(EntityTypeBuilder<BookModel> builder)
{
// Configure the table name
builder.ToTable("Books");
// Configure the primary key
builder.HasKey(x => x.Id);
// Configure properties
builder.Property(x => x.Id).ValueGeneratedOnAdd();
builder.Property(x => x.Title).IsRequired().HasMaxLength(100);
builder.Property(x => x.Author).IsRequired().HasMaxLength(100);
builder.Property(x => x.Description).IsRequired().HasMaxLength(500);
builder.Property(x => x.Category).IsRequired().HasMaxLength(100);
builder.Property(x => x.Language).IsRequired().HasMaxLength(50);
builder.Property(x => x.TotalPages).IsRequired();
// Seed data
builder.HasData(
new BookModel
{
Id = Guid.NewGuid(),
Title = "The Alchemist",
Author = "Paulo Coelho",
Description = "The Alchemist follows the journey of an Andalusian shepherd",
Category = "Fiction",
Language = "English",
TotalPages = 208
},
new BookModel
{
Id = Guid.NewGuid(),
Title = "To Kill a Mockingbird",
Author = "Harper Lee",
Description = "A novel about the serious issues of rape and racial inequality.",
Category = "Fiction",
Language = "English",
TotalPages = 281
},
new BookModel
{
Id = Guid.NewGuid(),
Title = "1984",
Author = "George Orwell",
Description = "A dystopian social science fiction novel and cautionary tale about the dangers of totalitarianism. ",
Category = "Fiction",
Language = "English",
TotalPages = 328
}
);
}
}
}
Let's break down the code above:
In Entity Framework Core, you can use the IEntityTypeConfiguration
interface to configure the entity type and seed data for the database. The BookTypeConfigurations
class implements the IEntityTypeConfiguration<BookModel>
interface and provides the configuration for the BookModel
entity.
- Configure Method: This method is used to configure the
BookModel
entity type. It defines the table name, primary key, and properties for theBookModel
entity.- Table Name: The
ToTable
method specifies the name of the table to be created in the database. In this case, the table name is set to "Books". - Primary Key: The
HasKey
method specifies the primary key for theBookModel
entity. The primary key is set to theId
property. - Properties: The
Property
method configures the properties of theBookModel
entity. It specifies the data type, length, and constraints for each property. - Seed Data: The
HasData
method seeds the database with initial data. It creates threeBookModel
objects with sample data for testing the API endpoints.
- Table Name: The
Now that we have created the BookTypeConfigurations
class, we need to register this configuration in the ApplicationContext
class. This ensures that the configuration is applied when the database is created or migrated.
We’re finally almost ready to test our API. But before we do that, we need to perform migrations to create the database and apply the seed data.
Remember that we added our database connection string in the appsettings.json
file? Now let's perform a migration and later update our database for the migration to take effect.