From 035973238581eaecd9b3d4adfb361ca75b20e654 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Mon, 21 Jul 2025 13:06:27 -0500 Subject: [PATCH] Updated Program Updated Program to have SQLite Configuration done through Connection Strings. Added auto migration for the database, if we get an exception, we create the database instead (As it doesn't exist.) --- FreeTubeSync/Program.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/FreeTubeSync/Program.cs b/FreeTubeSync/Program.cs index 6643e4a..8f93e25 100644 --- a/FreeTubeSync/Program.cs +++ b/FreeTubeSync/Program.cs @@ -1,13 +1,15 @@ using FreeTubeSync; using FreeTubeSync.Database; using FreeTubeSync.EndPoints; +using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddDbContext(); +builder.Services.AddDbContext(options => + options.UseSqlite(builder.Configuration.GetConnectionString("FreeTubeSync"))); builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); @@ -21,4 +23,17 @@ app.MapProfileEndpoints(); app.MapSearchHistoryEndpoints(); app.MapSettingEndpoints(); +await using(var serviceScope = app.Services.CreateAsyncScope()) +await using (var dbContext = serviceScope.ServiceProvider.GetRequiredService()) +{ + try + { + await dbContext.Database.MigrateAsync(); + } + catch (Exception e) + { + await dbContext.Database.EnsureCreatedAsync(); + } +} + app.Run(); \ No newline at end of file