From 8e920440a13e0f18b7f11107eab157df98a5dfb2 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 5 May 2024 02:21:04 +0100 Subject: [PATCH] Add automatic migration. --- Aaru.Server.New/Program.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Aaru.Server.New/Program.cs b/Aaru.Server.New/Program.cs index 4711199a..4da3f273 100644 --- a/Aaru.Server.New/Program.cs +++ b/Aaru.Server.New/Program.cs @@ -144,4 +144,33 @@ app.MapRazorComponents().AddInteractiveServerRenderMode(); // Add additional endpoints required by the Identity /Account Razor components. app.MapAdditionalIdentityEndpoints(); +using(IServiceScope scope = app.Services.CreateScope()) +{ + IServiceProvider services = scope.ServiceProvider; + + try + { + Stopwatch stopwatch = new(); + + stopwatch.Start(); + Console.WriteLine("\u001b[31;1mUpdating database with Entity Framework...\u001b[0m"); + DbContext context = services.GetRequiredService(); + await context.Database.MigrateAsync(); + stopwatch.Stop(); + + Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m", + stopwatch.Elapsed.TotalSeconds); + } + catch(Exception ex) + { + Console.WriteLine("\u001b[31;1mCould not open database...\u001b[0m"); +#if DEBUG + Console.WriteLine("\u001b[31;1mException: {0}\u001b[0m", ex.Message); +#endif + return; + } +} + +Console.WriteLine("\u001b[31;1mStarting web server...\u001b[0m"); + app.Run(); \ No newline at end of file