Other

EF Core data seeding: HasData, UseSeeding and your own seeder class

DEV Community: #efcore Remigiusz Zalewski
Other

The post argues that seed data is not one problem, and demonstrates three EF Core approaches side by side in a .NET 10 expense tracker API. Fixed reference data, app-required defaults and local fake data each get the right tool, with no duplicated rows on a second run.

Key points

  • Microsoft docs now recommend UseSeeding as the default and call HasData model managed data.
  • HasData rows need explicit primary keys, since the model snapshot tracks each row by key.
  • Every HasData edit costs a migration that emits InsertData or UpdateData statements.
  • UseSeeding covers synchronous paths, UseAsyncSeeding covers MigrateAsync and EnsureCreatedAsync; wire both.
  • An idempotent UseSeeding that inserts only missing names is safe on every startup and deployment.
  • A Bogus-based DevDataSeeder with UseSeed(42) produces deterministic rows and runs in development only.

Real-world use

  • Reference tables like currency or country lists that must be identical in every environment
  • An API where users add their own rows but default categories must always exist
  • A dev environment where every team member sees the same generated fake data
  • A deployment that applies migrations through a migration bundle in CI/CD

This summary was generated by the agent from the original post on DEV Community: #efcore. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message