Skip to content

EF Core Seed Data — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about EF Core Seed Data. We cover key concepts, practical examples, and best practices.

Your application needs initial data — reference values, admin users, default categories. You write a SQL script that runs during deployment, but it drifts from your model. EF Core seeding lets you populate data as part of the migration or application startup.

Wrong

// Manual SQL script that doesn't match the model
// INSERT INTO Categories VALUES ('Electronics', 'Books', 'Clothing')
// — hard to maintain, no type safety

Output: Script runs but silently fails if columns change. No compile-time checking.

Model seed data in migration configuration:

public class AppDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Category>().HasData(
            new Category { Id = 1, Name = "Electronics" },
            new Category { Id = 2, Name = "Books" },
            new Category { Id = 3, Name = "Clothing" }
        );
    }
}

Add a migration to apply seed data:

dotnet ef migrations add SeedCategories

For application startup seeding (development only):

public static async Task SeedAsync(AppDbContext db)
{
    if (!await db.Categories.AnyAsync())
    {
        db.Categories.AddRange(
            new Category { Name = "Electronics" },
            new Category { Name = "Books" }
        );
        await db.SaveChangesAsync();
    }
}

Prevention

  • Use HasData() in OnModelCreating for seed data in migrations.
  • Specify explicit primary key values in HasData() — EF Core needs them for diff tracking.
  • Use EnsureCreated() only for prototyping — not for production seeding.
  • Use dedicated seed methods in Program.cs for development data.
  • Do not seed data in every migration — create one seed migration per dataset.
  • Consider JSON files or configuration for large seed datasets.
  • Use db.Database.EnsureCreated() followed by seeding for new databases.

Common Mistakes with core seed data

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I update seed data after it was applied?

Yes. Change the data in HasData() and add a new migration. EF Core generates UPDATE statements for changes and DELETE/INSERT for removed entries. Primary key values must remain stable for EF Core to track changes.

Should I seed data in migrations or application startup?

Use migrations for reference/static data that should be versioned with the schema. Use application startup for development-only sample data. Migrations seed data is transactional with the schema change — startup seeding is not.

What happens if seed data already exists?

HasData() in migrations checks if the data exists by primary key. If the key exists, it updates. If not, it inserts. In startup seeding, use AnyAsync() to check before inserting to avoid duplicates on restart.

Learn more about EF Core data seeding at DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro