79675446

Date: 2025-06-22 21:05:22
Score: 2.5
Natty:
Report link

EF Core 9.0 Stills with this anoying issue!

My connection string is right, and it stills failing trying to check the pending migrations, a 'easy'task in theory. The database already exists but stills trying to create a new one and throws an exception. But in local environment works, the problem is when the app (in a docker image) is deployed to a remote VPS.

 public static IApplicationBuilder RunDbMigrations(this IApplicationBuilder app)
 {
     using (var scope = app.ApplicationServices.CreateScope())
     {
         var dbMaster = scope.ServiceProvider.GetRequiredService<MasterDbContext>();
         var pendingMigrations = dbMaster.Database.GetPendingMigrations();  // IT DOESN'T WORK

         string cs = dbMaster.Database.GetConnectionString();
         Console.WriteLine(!string.IsNullOrWhiteSpace(cs) ? $"Usando DB: {cs}" : "SIN CONEXION");

         if (!pendingMigrations.Any())
         {
             Console.WriteLine("No hay migraciones pendientes...");
             return app;
         }

         try
         {
             Console.WriteLine($"Aplicando migrations...");
             dbMaster.Database.Migrate();
         }
         catch (Exception ex)
         {
             Console.WriteLine($"Error al aplicar migraciones!: {ex.Message}");
             throw;
         }

         return app;
     }
 }

RunDbMigrations is called from program.cs after build line:

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.UseCors("Development"); // Enable CORS in development
}

// DB Migrations:
app.RunDbMigrations();

// http requests custom logging:
app.UseHttpRequestsLogging();

Please help!

Reasons:
  • RegEx Blacklisted phrase (3): Please help
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: thezuliano