© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2mo ago•
85 replies
Balte

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

advanced
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.");
            }
        }
        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
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
GitHub - baltermia/study-companion
Contribute to baltermia/study-companion development by creating an account on GitHub.
GitHub - baltermia/study-companion
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

❔ EF Core migrations
C#CC# / help
4y ago
EF-Core migrations issue
C#CC# / help
2y ago
❔ EF Postgres migration fail
C#CC# / help
3y ago
✅ EF Core migrations with Docker
C#CC# / help
11mo ago