According to MSDN, one may utilize the fact that migrations implmenet IDisposable like this.
using IServiceScope scope = app.Services.CreateScope();
scope.ServiceProvider.GetRequiredService<Context>()
.Database.Migrate();
However, I usually call the migration as follows and have never experienced any issues.
app.Services.CreateScope()
.ServiceProvider.GetRequiredService<Context>()
.Database.Migrate();
Is there a good reason in practice to worry? I figure, the migration is carried out once only, at the start and in most cases doesn't even carry out any changes, hence occupying the requested instance of the scope minimal amount of time. It's not asynchronous, neither, which to me additionally lowers the likelihood of it running amok.
Please note that I'm not advocating to omit using in that case. I understand the importance of following best-practices. But I'm also curious how important it is pragmatically.