[Blazor] Add database.

This commit is contained in:
2025-07-26 21:16:23 +01:00
parent 00d005ba98
commit dc4646512a
2 changed files with 56 additions and 12 deletions

View File

@@ -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<Context>(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<App>().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<Context>();
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();

View File

@@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components"/>
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons"/>
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.AspNetCore" />
<PackageReference Include="Serilog.Sinks.Console" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite"/>
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components"/>
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons"/>
<PackageReference Include="Serilog"/>
<PackageReference Include="Serilog.AspNetCore"/>
<PackageReference Include="Serilog.Sinks.Console"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RomRepoMgr.Database\RomRepoMgr.Database.csproj"/>
</ItemGroup>
</Project>