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

BeginnerIntermediateAdvancedProfessional

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

  1. Seed two categories and three products with explicit Ids.
  2. Add migration and inspect seed INSERTs in Up().
  3. Update database and SELECT from Products in SSMS.
  4. Add HasData for a default Customer and link Order seed row.
  5. 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.

Mid PDF Detailed
How do you seed initial data using migrations / EF Core configuration?
Short answer: Use the HasData() method in OnModelCreating(): modelBuilder.Entity&lt;User&gt;().HasData( new User { Id = 1, Name = &quot;Admin&quot; } ); Data is inserted automatically during Update-Database. Changes to s…
Junior PDF Detailed
What is Database First approach?
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to vers…
Junior PDF Detailed
What is Database First approach?
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy data…
Mid PDF Detailed
How do you reverse engineer a database in EF Core (DB First) into entity classes & DbContext?
Short answer: Use the CLI or Package Manager Console: dotnet ef dbcontext scaffold &quot;YourConnectionString&quot; Microsoft.EntityFrameworkCore.SqlServer -o Models OR Scaffold-DbContext &quot;YourConnectionString&quot;…
Mid PDF Detailed
How does Code First handle schema changes in the database?
Short answer: EF Core uses migrations to track and apply schema changes: You modify your C# models. Use Add-Migration to create a migration class. Use Update-Database to apply the changes to the DB. EF Core compares the…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details