diff --git a/RomRepoMgr.Blazor/Program.cs b/RomRepoMgr.Blazor/Program.cs index dffc99e..72afc9a 100644 --- a/RomRepoMgr.Blazor/Program.cs +++ b/RomRepoMgr.Blazor/Program.cs @@ -1,6 +1,9 @@ +using System.Diagnostics; +using Microsoft.EntityFrameworkCore; using Microsoft.FluentUI.AspNetCore.Components; using RomRepoMgr.Blazor; using RomRepoMgr.Blazor.Components; +using RomRepoMgr.Database; using Serilog; Log.Logger = new LoggerConfiguration() @@ -53,6 +56,19 @@ builder.Host.UseSerilog(); // ✅ Plug Serilog into the host builder.Services.AddRazorComponents().AddInteractiveServerComponents(); builder.Services.AddFluentUIComponents(); +Log.Debug("Creating database context..."); + +builder.Services.AddDbContextFactory(options => +{ + options.UseSqlite($"Data Source={Consts.DbFolder}/database.db"); +#if DEBUG + options.EnableSensitiveDataLogging(); + options.LogTo(Log.Debug); +#else + options.LogTo(Log.Information, LogLevel.Information); +#endif +}); + Log.Debug("Building the application..."); WebApplication app = builder.Build(); @@ -70,5 +86,28 @@ app.UseAntiforgery(); app.MapStaticAssets(); app.MapRazorComponents().AddInteractiveServerRenderMode(); +Stopwatch stopwatch = new(); + +using(IServiceScope scope = app.Services.CreateScope()) +{ + IServiceProvider services = scope.ServiceProvider; + + try + { + Log.Information("Updating the database..."); + stopwatch.Start(); + Context dbContext = services.GetRequiredService(); + await dbContext.Database.MigrateAsync(); + stopwatch.Stop(); + Log.Debug("Database migration: {Elapsed} seconds", stopwatch.Elapsed.TotalSeconds); + } + catch(Exception ex) + { + Log.Error(ex, "An error occurred while updating the database"); + + return; + } +} + Log.Debug("Running the application..."); app.Run(); \ No newline at end of file diff --git a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj index 2142b22..2535e02 100644 --- a/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj +++ b/RomRepoMgr.Blazor/RomRepoMgr.Blazor.csproj @@ -1,16 +1,21 @@ - - net9.0 - enable - enable - + + net9.0 + enable + enable + - - - - - - - + + + + + + + + + + + +