using Infrastructure; using Infrastructure.Data; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddInfrastructureServices(builder.Configuration); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; var dbContext = services.GetRequiredService(); // Directly resolving your DbContext EnsureDatabaseCreated(dbContext); } app.Run(); static void EnsureDatabaseCreated(HealthcareManagerContext dbContext) { // Checking for pending migrations is more efficient than applying migrations unconditionally if (dbContext.Database.GetPendingMigrations().Any()) { dbContext.Database.Migrate(); } }