C#C
C#7d ago
Balte

.NET 10 EF Core Postgres Migrations always Fail, __MigrationsHistory doesn't get filled

I'm so frustrated, I've already wasted the last hour trying to fix this.

So I migrate the DB at the beginning like this:

        await using (AsyncServiceScope scope = bot.Services.CreateAsyncScope())
        {
            try
            {
                await using PostgresDbContext db = scope.ServiceProvider.GetRequiredService<PostgresDbContext>();
                
                await db.Database.EnsureCreatedAsync();

                IEnumerable<string> pendingMigrations = await db.Database.GetPendingMigrationsAsync();
                
                if (pendingMigrations.Any())
                {
                    Console.WriteLine("Applying pending migrations...");
                    await db.Database.MigrateAsync();
                    Console.WriteLine("Migrations applied successfully.");
                }
                else
                {
                    Console.WriteLine("No pending migrations found.");
                }
                
            }
            catch (Exception ex)
            {
                ILogger<StartCommand> logger = scope.ServiceProvider.GetRequiredService<ILogger<StartCommand>>();
                logger.LogError(ex, "An error occurred while migrating or initializing the database.");
            }
        }


When I ran it locally it always worked. I had multiple migrations so I thought maybe that was the issue. It seemed like the Calender Table was added multiple times. But removing all Migrations and adding a Single one didn't change anything.

When running it completely new using my docker compose it threw the exceptions... You can gladly check my repo here and run it yourself: https://github.com/baltermia/study-companion

The error is attached as a file (discord did that automatiacally somehow)

Any help will be well appereciated🙏
GitHub
Contribute to baltermia/study-companion development by creating an account on GitHub.
Was this page helpful?