mirror of
https://github.com/claunia/romrepomgr.git
synced 2025-12-16 19:24:51 +00:00
[Blazor] Add database.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.FluentUI.AspNetCore.Components;
|
using Microsoft.FluentUI.AspNetCore.Components;
|
||||||
using RomRepoMgr.Blazor;
|
using RomRepoMgr.Blazor;
|
||||||
using RomRepoMgr.Blazor.Components;
|
using RomRepoMgr.Blazor.Components;
|
||||||
|
using RomRepoMgr.Database;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
Log.Logger = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
@@ -53,6 +56,19 @@ builder.Host.UseSerilog(); // ✅ Plug Serilog into the host
|
|||||||
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
|
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
|
||||||
builder.Services.AddFluentUIComponents();
|
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...");
|
Log.Debug("Building the application...");
|
||||||
WebApplication app = builder.Build();
|
WebApplication app = builder.Build();
|
||||||
|
|
||||||
@@ -70,5 +86,28 @@ app.UseAntiforgery();
|
|||||||
app.MapStaticAssets();
|
app.MapStaticAssets();
|
||||||
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
|
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...");
|
Log.Debug("Running the application...");
|
||||||
app.Run();
|
app.Run();
|
||||||
@@ -1,16 +1,21 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components"/>
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite"/>
|
||||||
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons"/>
|
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components"/>
|
||||||
<PackageReference Include="Serilog" />
|
<PackageReference Include="Microsoft.FluentUI.AspNetCore.Components.Icons"/>
|
||||||
<PackageReference Include="Serilog.AspNetCore" />
|
<PackageReference Include="Serilog"/>
|
||||||
<PackageReference Include="Serilog.Sinks.Console" />
|
<PackageReference Include="Serilog.AspNetCore"/>
|
||||||
</ItemGroup>
|
<PackageReference Include="Serilog.Sinks.Console"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\RomRepoMgr.Database\RomRepoMgr.Database.csproj"/>
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user