A task:
When starting the application, you need to check if there is a default user in the database, if not, then create a user.
How to make and where to write such a check so that it is executed only once at the start of the application?
It is necessary to be able to work with scoped services.
I thought about IHostedService service, but it is a singleton.
One option is to write the code in a Program.cs
var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<WeatherDbContext>();
// do something
}
But if there is a lot of code, then posting it here is not correct.
How to move the code to some class, and then only its call remains?