After the question got updated and it's clear that it's going about EntityFramework Core migrations, there is an semi-official way form the ASP.NET Core team on how to correctly resolve the DbContext
during application startup.
By default, the DbContext
is registered as scoped service, so you get instantiated once per request. The catch during the application startup is, that there is no context yet and only app.ApplicationServices
is available within Configure
method and the ApplicationServices
provider is essentially resolving singletons (scoped is a singleton per scope and application scope lives as long as the application does).
So the trick is to create a scope first, resolve the DbContext
, do the operations and then dispose the context (and the DbContext
with it).
Examples can be found in the MusicStore example application here and here.
As of right now, this is the only safe way to resolve it w/o causing any issues with i.e. disposed object exceptions.
Also please note, that the earliest point you can do this is in the Configure
method, because only then the IoC container has been built. In ConfigureServices
you only populate the IServiceCollection
.
The relevant code snippets:
void Configure(IApplicationBuilder app)
{
//Populates the MusicStore sample data
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
}
public static class SampleData
{
...
public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider, bool createUsers = true)
{
using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var db = serviceScope.ServiceProvider.GetService<MusicStoreContext>();
if (await db.Database.EnsureCreatedAsync())
{
await InsertTestData(serviceProvider);
if (createUsers)
{
await CreateAdminUser(serviceProvider);
}
}
}
}
}
Edit:
Additional resources regarding this issue: