using Infrastructure; using Microsoft.EntityFrameworkCore; namespace Controllers.Extensions; public static class DatabaseSetupExtension { public static void AddDbConnectionService(this IServiceCollection services, IConfiguration config) { var connectionString = config.GetConnectionString("DefaultConnection") ?? throw new ArgumentException("Нет строки подключения"); services.AddDbContext(options => options.UseNpgsql(connectionString)); services.AddSingleton, DbContextFactory>(); } public static void MigrateDb(this IApplicationBuilder app) { try { using var scope = app.ApplicationServices.CreateScope(); var context = scope.ServiceProvider.GetRequiredService>(); using var db = context.CreateDbContext(); db.Database.Migrate(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }