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!