using Backend.Data.Interfaces; using System; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Backend.Data.Config { public static class DatabaseConfiguration { public static IServiceCollection AddDatabaseModule(this IServiceCollection @this, IConfiguration configuration) { var connection = configuration.GetConnectionString("AppDbContext"); @this.AddDbContextPool(options => options.UseMySql(connection, ServerVersion.AutoDetect(connection))); return @this; } public static IApplicationBuilder UseApplicationDatabase(this IApplicationBuilder @this, IServiceProvider serviceProvider) { using (var scope = serviceProvider.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); context.Database.OpenConnection(); context.Database.Migrate(); } return @this; } } }