| 6 | namespace Common; |
| 7 | |
| 8 | public static class MigrationRunner |
| 9 | { |
| 10 | public static async Task MigrateDbContextAsync<TContext>(this IHost host) |
| 11 | where TContext : DbContext |
| 12 | { |
| 13 | using var scope = host.Services.CreateScope(); |
| 14 | var services = scope.ServiceProvider; |
| 15 | var loggerFactory = services.GetRequiredService<ILoggerFactory>(); |
| 16 | var logger = loggerFactory.CreateLogger(typeof(MigrationRunner)); |
| 17 | try |
| 18 | { |
| 19 | var context = services.GetRequiredService<TContext>(); |
| 20 | await context.Database.MigrateAsync(); |
| 21 | } |
| 22 | catch (Exception ex) |
| 23 | { |
| 24 | logger.LogError(ex, "An error occured during migration"); |
| 25 | } |
| 26 | |
| 27 | logger.LogInformation("✅ Migration complete for {Name}", typeof(TContext).Name); |
| 28 | } |
| 29 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…