Search code examples
c#asp.net-coreentity-framework-coreentity-framework-migrations.net-8.0

.NET Core / Entity Framework Core not updating, seemingly referencing a migration history database even when it doesn't exist


dotnet ef migrations add X

This works and adds three files (if there are no other migrations at all) in the migration folder, X.cs, X.Designer.cs and QuizzDbContextModelSnapshot.cs.

dotnet ef database update 

however always returns the same result:

dotnet ef database update

Build started...
Build succeeded.

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [MigrationId], [ProductVersion]
      FROM [__EFMigrationsHistory]
      ORDER BY [MigrationId];
info: Microsoft.EntityFrameworkCore.Migrations[20405]
      No migrations were applied. The database is already up to date.

No migrations were applied. The database is already up to date.
Done.

dotnet ef database update creates a database if it doesn't already exist, but all there exists inside it is the __EFMigrationsHistory table.

What I have tried:

  • Deleted the migrations folder
  • Deleted the migration history table
  • Deleted the database
  • Created a new database
  • Created a new server (changing the connection strings in appsettings.json of course)

None of these attempts worked.

Weirdly, dotnet ef migrations list doesn't seem to work either

dotnet ef migrations list

Build started...
Build succeeded.

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [MigrationId], [ProductVersion]
      FROM [__EFMigrationsHistory]
      ORDER BY [MigrationId];
No migrations were found.
dotnet ef migrations remove

Build started...
Build succeeded.

No ModelSnapshot was found.

I also tried reverting my QuizzDbContext.cs to previous commits, but didn't do anything. Right now it's structured in the following way:

using API.Models;
using API.Models.Domain;
using API.Models.Domain.Auth;
using API.Models.Domain.Questions;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Question = API.Models.Domain.Questions.Question;
using Microsoft.AspNetCore.Identity;
using API.Models.Domain.Extra;

namespace API.Data
{
    public class QuizzDbContext : IdentityDbContext<ApplicationUser>
    {
        public QuizzDbContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Models.Domain.Questions.Question> Questions { get; set; }
        public DbSet<Test> Tests { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Question>()
                 .HasDiscriminator<string>("QuestionType")
                 .HasValue<MultipleChoiceQuestion>("MultipleChoice")
                 .HasValue<TrueFalseQuestion>("TrueFalse")
                            .HasValue<Question>("Base");

            modelBuilder.Entity<Test>()
               .HasMany(t => t.Questions)
               .WithMany();
        }

        public DbSet<API.Models.Domain.Answer> Answer { get; set; } = default!;
        public DbSet<Framework> Frameworks { get; set; }
    }
}

I'm using .NET 8.0, and the following packages:

enter image description here


Solution

  • Moving the the whole solution/project to a different folder, refactoring everything fixed the problem. I do not know why it did so but migrations work now.

    If I had to guess, invalid path or ghost files.

    edit: What was happening is that the visual studio Clean/rebuild wasn't cleaning some old files! so if you encounter this bug and you've tried everything else, make sure to delete obj and bin manually. Make sure to backup just in case etc etc!