Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way for applying EF Core 9's UseSeeding in Non-Local Environments?

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:

  • When EnsureCreated is called
  • when Migrate() is called
  • When dotnet 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?

like image 645
S. ten Brinke Avatar asked Dec 17 '25 21:12

S. ten Brinke


1 Answers

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.

like image 150
Steve Avatar answered Dec 19 '25 13:12

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!