Entity Framework Core Interview Question #96
What is data seeding with HasData in EF Core?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
HasData configures model-managed seed data. EF Core migrations detect changes to this configured data and can generate insert, update, or delete operations.
Detailed Explanation
HasData configures model-managed seed data. EF Core migrations detect changes to this configured data and can generate insert, update, or delete operations.
It is suitable for small, stable reference data whose values are part of the model's expected database state. Seed values generally need explicit primary keys.
Large content datasets, environment-dependent data, or complex initialization workflows are often better handled by application-specific seeding code rather than HasData.
Code Example
modelBuilder.Entity<Status>().HasData(
new Status { Id = 1, Name = "Active" },
new Status { Id = 2, Name = "Inactive" }
);