using System; using InternshipSystem.Repository; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.DependencyInjection; namespace InternshipSystem.Api.Extensions { public static class ApplicationBuilderExtensions { public static IApplicationBuilder UseMigration(this IApplicationBuilder app) { bool done = false; while (!done) { try { using var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope(); using var context = serviceScope.ServiceProvider.GetService(); context.Database.Migrate(); done = true; } catch (Exception) { // ignored } } return app; } public static IApplicationBuilder UseDefaultData(this IApplicationBuilder app, bool useDefaultData) { using var serviceScope = app.ApplicationServices.GetRequiredService().CreateScope(); using var context = serviceScope.ServiceProvider.GetService(); var filler = serviceScope.ServiceProvider.GetService(); context.Database.Migrate(); if (useDefaultData && !context.Editions.Any()) { filler.FillAll().Wait(); } return app; } } }