efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Wrong output of GetAppliedMigrations()

Open aminkosh opened this issue 5 years ago • 2 comments

Hi,

I manually removed one of the entries from the ____EFMigrationsHistory. But the GetAppliedMigrations() gives me still the removed migration.

The count here should be 398 but it is 399

_tenantRepo.GetDbContext().Database.GetAppliedMigrations().Count()

Any help appreciated

EF Core version:Core 3.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET Core 3.1 Operating system: win 10 IDE: Visual Studio 2019 16.3

aminkosh avatar Jul 29 '21 08:07 aminkosh

@aminkosh I am not able to reproduce this. I created three migrations for the code below. GetAppliedMigrations returned 3. I then deleted the second migration. GetAppliedMigrations returned 2.

Are you sure that the row that was deleted represented a migration for the current DbContext, and not some other context?

image

image

public class Blog
{
	public int Id { get; set; }
	public string Name { get; set; }
	public string Description { get; set; }
}

public class SomeDbContext : DbContext
{
    private static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole()); //.SetMinimumLevel(LogLevel.Information));

    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Your.ConnectionString)
            .UseLoggerFactory(ContextLoggerFactory)
            .EnableSensitiveDataLogging();
}

public class Program
{
    public static void Main()
    {
        using (var context = new SomeDbContext())
        {
            Console.WriteLine(context.Database.GetAppliedMigrations().Count());
        }
    }
}

ajcvickers avatar Aug 01 '21 11:08 ajcvickers

Ahh you are right . The unit of work was bugy and it could not recognize the context. Thanks a lot .

Arthur Vickers @.***> schrieb am So. 1. Aug. 2021 um 13:25:

@aminkosh https://github.com/aminkosh I am not able to reproduce this. I created three migrations for the code below. GetAppliedMigrations returned 3. I then deleted the second migration. GetAppliedMigrations returned 2.

Are you sure that the row that was deleted represented a migration for the current DbContext, and not some other context?

[image: image] https://user-images.githubusercontent.com/1430078/127769086-54a5b82a-b9a0-475e-8b3d-6940b8c70a3d.png

[image: image] https://user-images.githubusercontent.com/1430078/127769091-a99c4793-25c8-4003-9378-6ce46c26403b.png

public class Blog { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public class SomeDbContext : DbContext { private static ILoggerFactory ContextLoggerFactory => LoggerFactory.Create(b => b.AddConsole()); //.SetMinimumLevel(LogLevel.Information));

public DbSet<Blog> Blogs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseSqlServer(Your.ConnectionString)
        .UseLoggerFactory(ContextLoggerFactory)
        .EnableSensitiveDataLogging();

} public class Program { public static void Main() { using (var context = new SomeDbContext()) { Console.WriteLine(context.Database.GetAppliedMigrations().Count()); } } }

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/efcore/issues/25358#issuecomment-890501729, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEHL7SVE4DA7CC366C3A5WLT2UVKZANCNFSM5BF6UMFA .

aminkosh avatar Aug 01 '21 15:08 aminkosh