Don't dispose database context for server processes

net8.0
Pat Hartl 2023-11-17 12:57:23 -06:00
parent 6b005eb384
commit 09d23bcb78
1 changed files with 7 additions and 8 deletions

View File

@ -228,17 +228,16 @@ namespace LANCommander
// Migrate // Migrate
Logger.Debug("Migrating database if required"); Logger.Debug("Migrating database if required");
await using var scope = app.Services.CreateAsyncScope(); await using var scope = app.Services.CreateAsyncScope();
using (var db = scope.ServiceProvider.GetService<DatabaseContext>()) using var db = scope.ServiceProvider.GetService<DatabaseContext>();
if ((await db.Database.GetPendingMigrationsAsync()).Any())
{ {
if ((await db.Database.GetPendingMigrationsAsync()).Any()) var dataSource = new SqliteConnectionStringBuilder(settings.DatabaseConnectionString).DataSource;
{
var dataSource = new SqliteConnectionStringBuilder(settings.DatabaseConnectionString).DataSource;
if (File.Exists(dataSource)) if (File.Exists(dataSource))
File.Copy(dataSource, Path.Combine("Backups", $"LANCommander.db.{DateTime.Now.ToString("dd-MM-yyyy-HH.mm.ss.bak")}")); File.Copy(dataSource, Path.Combine("Backups", $"LANCommander.db.{DateTime.Now.ToString("dd-MM-yyyy-HH.mm.ss.bak")}"));
await db.Database.MigrateAsync(); await db.Database.MigrateAsync();
}
} }
// Autostart any server processes // Autostart any server processes