Tutorials Entity Framework Core Tutorial
Seed Data in EF Core
Seed Data in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 17 of 100
Seed Data in EF Core
Beginner → Intermediate → Advanced → Professional
Beginner · 1 — Foundations · ~6 min · Module 2: Code First Approach
What is this?
Seed data inserts baseline rows — categories, sample products, admin customer — via HasData in OnModelCreating or a dedicated IDataSeeder run at startup.
Why should you care?
Empty ShopNest catalog makes UI development impossible; seed gives every developer and CI pipeline the same starting products.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
modelBuilder.Entity<Category>().HasData(
new Category { Id = 1, Name = "Electronics" },
new Category { Id = 2, Name = "Fashion" });
modelBuilder.Entity<Product>().HasData(
new Product { Id = 1, Name = "Wireless Mouse", Price = 499, CategoryId = 1, Sku = "SN-MSE-01" });
What happened?
- HasData embeds INSERT statements in migrations with fixed Ids so repeats are deterministic.
- Changes require a new migration.
Practice next
- Seed two categories and three products with explicit Ids.
- Add migration and inspect seed INSERTs in Up().
- Update database and SELECT from Products in SSMS.
- Add HasData for a default Customer and link Order seed row.
- Move seed to DbContext.OnModelCreating vs IEntityTypeConfiguration — same outcome.
Remember
HasData seeds via migrations. Stable Ids keep environments consistent. Runtime seeders suit larger or environment-specific data.
ShopNest demo catalog
QA environment applies migrations and instantly has Electronics/Fashion categories for regression tests.
Outcome: Automated UI tests never fail on empty catalog pages.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!