EF Core 9 introduces a new feature for seeding data which is called UseSeeding.
Compared to HasData, also now known as "Model Managed Data", UseSeeding is not part of your migrations.
The documentation says that UseSeeding runs in the following scenarios:
EnsureCreated is calledMigrate() is calleddotnet ef database update is called.This is fine for development scenarios. A developer runs dotnet ef database update to create their local database and UseSeeding will then be used to fill the database.
However, the docs discourage using these strategies for non-local scenarios, for reasons mentioned in the docs.
My question is: What is the recommended way to combine the recommended way of applying migrations (generating a SQL script based on your migrations) with the recommended way of applying seed data (UseSeeding) for non-local environments?
I was looking for a very similar solution, and found dotnet ef migrations bundle to be quite useful. Because it effectively runs dotnet ef database update, it will also include any logic in your UseSeeding and UseSeedingAsync functions. That is one of the ways those methods get applied according to the docs.
I also found it valuable because it doesn't require any extra dependencies if you build the bundle with --self-contained.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With