using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; namespace Microsoft.AspNetCore.Hosting { public static class IWebHostExtensions { public static IWebHost MigrateDbContext(this IWebHost webHost, Action seeder) where TContext : DbContext { using (var scope = webHost.Services.CreateScope()) { var services = scope.ServiceProvider; var logger = services.GetRequiredService>(); var context = services.GetService(); try { logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}"); context.Database .Migrate(); seeder(context,services); logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}"); } catch (Exception ex) { logger.LogError(ex, $"An error occurred while migrating the database used on context {typeof(TContext).Name}"); } } return webHost; } } }