From 69d5ecf0d767bfc7452a916ae896b71360ec7cc5 Mon Sep 17 00:00:00 2001 From: masoud Date: Sun, 7 Dec 2025 21:27:46 +0000 Subject: [PATCH] revert: Back to simple MigrateAsync - it's already idempotent MigrateAsync is smart enough to: - Create DB if it doesn't exist - Apply only pending migrations - Skip if DB is up to date The orphaned CMS.mdf files were cleaned from SQL Server. --- .../ApplicationDbContextInitialiser.cs | 20 +------------------ 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/CMSMicroservice.Infrastructure/Persistence/ApplicationDbContextInitialiser.cs b/src/CMSMicroservice.Infrastructure/Persistence/ApplicationDbContextInitialiser.cs index 4b1b1d8..b7ca4d8 100644 --- a/src/CMSMicroservice.Infrastructure/Persistence/ApplicationDbContextInitialiser.cs +++ b/src/CMSMicroservice.Infrastructure/Persistence/ApplicationDbContextInitialiser.cs @@ -23,25 +23,7 @@ public class ApplicationDbContextInitialiser { if (_context.Database.IsSqlServer()) { - // Use EnsureCreated for initial setup, then apply migrations - // EnsureCreated is idempotent - safe to call multiple times - var created = await _context.Database.EnsureCreatedAsync(); - - if (!created) - { - // Database exists, check for pending migrations - _logger.LogInformation("Database already exists, checking for migrations..."); - var pending = await _context.Database.GetPendingMigrationsAsync(); - if (pending.Any()) - { - _logger.LogInformation("Applying {Count} pending migrations", pending.Count()); - await _context.Database.MigrateAsync(); - } - } - else - { - _logger.LogInformation("Database created successfully"); - } + await _context.Database.MigrateAsync(); } } catch (Exception ex)