using LendWise.Web.Data; using LendWise.Web.Models; using LendWise.Web.Services; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); builder.Services.AddSingleton(); builder.Services.Configure(builder.Configuration.GetSection("Site")); builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); var app = builder.Build(); // Create the database and tables on first run. If SQL Server is not reachable the site // still starts (all pages work); only saving forms will show a friendly error. using (var scope = app.Services.CreateScope()) { var logger = scope.ServiceProvider.GetRequiredService>(); try { var db = scope.ServiceProvider.GetRequiredService(); db.Database.EnsureCreated(); logger.LogInformation("Database is ready."); } catch (Exception ex) { logger.LogWarning(ex, "Could not reach SQL Server. Check ConnectionStrings:DefaultConnection in appsettings.json."); } } if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/error/500"); app.UseHsts(); } app.UseStatusCodePagesWithReExecute("/error/{0}"); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllers(); app.Run();