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<DatabaseContext>(options => options.UseNpgsql(connectionString)); services.AddSingleton<IDbContextFactory<DatabaseContext>, DbContextFactory>(); } public static void MigrateDb(this IApplicationBuilder app) { try { using var scope = app.ApplicationServices.CreateScope(); var context = scope.ServiceProvider.GetRequiredService<IDbContextFactory<DatabaseContext>>(); using var db = context.CreateDbContext(); db.Database.Migrate(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }