From 3b01f42f999b7a1588176dc02abb36e78c9a3ff4 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Sun, 26 Oct 2025 18:44:31 +0000 Subject: [PATCH] fix to startup :| --- TestHosts/TestHosts/Startup.cs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/TestHosts/TestHosts/Startup.cs b/TestHosts/TestHosts/Startup.cs index bc55fbe..321a0be 100644 --- a/TestHosts/TestHosts/Startup.cs +++ b/TestHosts/TestHosts/Startup.cs @@ -10,6 +10,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Shared.Logger; using TestHosts.Common; using TestHosts.Database.PataPawa; using TestHosts.Database.TestBank; @@ -111,34 +112,42 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken){ public sealed class DatabaseInitializerHostedService : IHostedService { private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; - public DatabaseInitializerHostedService(IServiceProvider serviceProvider, ILogger logger) + public DatabaseInitializerHostedService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; - _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { - _logger.LogInformation("Starting database initialization..."); + Logger.LogWarning("Starting database initialization..."); try { using var scope = _serviceProvider.CreateScope(); - var pataPawaContext = scope.ServiceProvider.GetRequiredService(); + PataPawaContext pataPawaContext = scope.ServiceProvider.GetRequiredService(); - // Example: apply migrations or seed data - await pataPawaContext.Database.MigrateAsync(cancellationToken); + if (pataPawaContext.Database.IsRelational()) { + // Example: apply migrations or seed data + await pataPawaContext.Database.MigrateAsync(cancellationToken); + } + //else { + // await pataPawaContext.Database.EnsureCreatedAsync(cancellationToken); + //} - var bankContext = scope.ServiceProvider.GetRequiredService(); - await bankContext.Database.MigrateAsync(cancellationToken); + TestBankContext bankContext = scope.ServiceProvider.GetRequiredService(); + if (bankContext.Database.IsRelational()) { + await bankContext.Database.MigrateAsync(cancellationToken); + } + //else { + // await bankContext.Database.EnsureCreatedAsync(cancellationToken); + //} - _logger.LogInformation("Database initialization completed successfully."); + Logger.LogWarning("Database initialization completed successfully."); } catch (Exception ex) { - _logger.LogError(ex, "Database initialization failed."); + Logger.LogError("Database initialization failed.", ex); throw; // Let the host fail fast if initialization is critical } }