In my Startup.cs I perform migrations like this. There's some weird issue in the private method I can't explain and it seems to be related to the asynchronous version of the invokation. The following works just as expected (although the asynchornicity in pointless.
public async void Configure(
IApplicationBuilder app, IWebHostEnvironment env)
{
...
await MigrateDb(app);
...
}
private static async Task MigrateDb(IApplicationBuilder app)
{
using IServiceScope scope = app.ApplicationServices
.GetService<IServiceScopeFactory>()?.CreateScope();
if (scope == null)
throw new OperationCanceledException();
Context context = scope.ServiceProvider.GetRequiredService<Context>();
context.Database.Migrate();
}
However, when I apply the asynchoronous migration, stuff stop to work. FOr some reason, the controllers are not reachable and I get 404's on every endpoint, including the ping that only returns a fixed string form the service without any contact with the DB.
private static async Task MigrateDb(IApplicationBuilder app)
{
using ...
Context context = ...
await context.Database.MigrateAsync();
}
Googling gave absolutely nothing related. I tried explicitly killing the scope by scope.Dispose();
. At the moment I have no idea what to google for more or how to approach the trouble-shooting. It's not a critical production issue - I'm only doing a test (and this part isn't the aim of it). Still, it would be very interesting to understand why this happens.