question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception creating migration to Drop a table Pomelo 6.0.1

See original GitHub issue

Hi, im getting an Exception when trying to generate a migration to drop a table

.net 6 EF Core 6.0.1 Pomelo 6.0.1

I removed the Model entity (iUW.Models.ApplicantProduct) from the DbContext , and removed any references to it from the codebase.

when I run

add-migration dropTableApplicantProduct -context DatabaseUnitOfWork

I get this stack trace

add-migration dropTableApplicantProduct -context DatabaseUnitOfWork
Build started...
Build succeeded.
System.ArgumentException: An item with the same key has already been added. Key: [0, Property: iUW.Models.ApplicantProduct (Dictionary<string, object>).ProductId (no field, int) Indexer Required FK Index]
   at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext()
   at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.PostFilterOperations(IEnumerable`1 migrationOperations)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.PostFilterOperations(IEnumerable`1 migrationOperations)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An item with the same key has already been added. Key: [0, Property: iUW.Models.ApplicantProduct (Dictionary<string, object>).ProductId (no field, int) Indexer Required FK Index]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
CliveBennettcommented, Feb 9, 2022

Great thanks for investigating @lauxjpn much appreciated.

0reactions
lauxjpncommented, Feb 8, 2022

@CliveBennett Looks like you found a bug in EF Core. This issue is also reproducible using the SQL Server provider:

Program.cs
// #define SECOND_MIGRATION

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace IssueConsoleTemplate
{
    public class Entity
    {
        [Required, Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
    }

    public class Applicant : Entity
    {
    }

    public class Product : Entity
    {
    }
    
#if !SECOND_MIGRATION
    public class ApplicantProduct : Entity
    {
        public int ProductId { get; set; }
        
        [ForeignKey("ProductId")]
        public virtual Product Product { get; set; }
        
        public int ApplicantId { get; set; }
        
        [ForeignKey("ApplicantId")]
        public virtual Applicant Applicant { get; set; }
    }
#endif
    
    public class Context : DbContext
    {
        public DbSet<Applicant> Applicants { get; set; }
        public DbSet<Product> Products { get; set; }
#if !SECOND_MIGRATION
        public DbSet<ApplicantProduct> ApplicantProducts { get; set; }
#endif
        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connectionString = "server=127.0.0.1;port=3308;user=root;password=;database=Issue1613";
            var serverVersion = ServerVersion.AutoDetect(connectionString);

            optionsBuilder
                // .UseMySql(connectionString, serverVersion)
                .UseSqlServer(@"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=Issue1613")
                .UseLoggerFactory(
                    LoggerFactory.Create(
                        b => b
                            .AddConsole()
                            .AddFilter(level => level >= LogLevel.Information)))
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
#if !SECOND_MIGRATION
            modelBuilder.Entity<ApplicantProduct>(
                entity =>
                {
                    entity.HasIndex(u => new { u.ApplicantId, u.ProductId })
                        .IsUnique();
                });
#endif
        }
    }

    internal static class Program
    {
        private static void Main()
        {
            //using var context = new Context();
        }
    }
}
Output
PS E:\Sources\PomeloIssues\Issue1613\IssueConsoleTemplate> dotnet ef migrations add 'RemoveTable'
Build started...
Build succeeded.
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
      Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 6.0.1 initialized 'Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.1' with options: SensitiveDataLoggingEnabled DetailedErrorsEnabled
System.ArgumentException: An item with the same key has already been added. Key: [0, Property: IssueConsoleTemplate.ApplicantProduct (Dictionary<string, object>).ProductId (no field, int) Indexer Required FK Index]
   at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
   at System.Collections.Generic.SortedSet`1.Add(T item)
   at System.Collections.Generic.SortedDictionary`2.Add(TKey key, TValue value)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table) in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 789
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table) in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 739
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext() in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 687
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext() in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 2432
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext) in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 200
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target) in E:\Sources\EFCore-6.0\src\EFCore.Relational\Migrations\Internal\MigrationsModelDiffer.cs:line 164
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language) in E:\Sources\EFCore-6.0\src\EFCore.Design\Migrations\Design\MigrationsScaffolder.cs:line 156
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace) in E:\Sources\EFCore-6.0\src\EFCore.Design\Design\Internal\MigrationsOperations.cs:line 113
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace) in E:\Sources\EFCore-6.0\src\EFCore.Design\Design\OperationExecutor.cs:line 185
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() in E:\Sources\EFCore-6.0\src\EFCore.Design\Design\OperationExecutor.cs:line 173
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0() in E:\Sources\EFCore-6.0\src\EFCore.Design\Design\OperationExecutor.cs:line 716
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) in E:\Sources\EFCore-6.0\src\EFCore.Design\Design\OperationExecutor.cs:line 699
An item with the same key has already been added. Key: [0, Property: IssueConsoleTemplate.ApplicantProduct (Dictionary<string, object>).ProductId (no field, int) Indexer Required FK Index]

/cc @bricelam

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception creating migration to Drop a table #27418
Hi, im getting an Exception when trying to generate a migration to drop a table .net 6. EF Core 6.0.1. Pomelo 6.0.1.
Read more >
.net 6.0 - Pomelo.EntityFrameworkCore.MySql problem ...
I fixed some of them but the last one, I couldn`t. System.InvalidOperationException: The property 'SqlClass.Disabled' is of type 'byte' which is ...
Read more >
Breaking changes in EF Core 6.0
The following table illustrates the changes for Migrations. OnDelete(), ON DELETE. NoAction, NO ACTION. ClientNoAction, NO ACTION. Restrict ...
Read more >
7.1 Entity Framework 6 Support
Idempotent migrations scripts allow you to generate an SQL script that can upgrade a database at any version up to the latest version....
Read more >
PomeloFoundation
How can we give Pomelo the hint it needs to stop using ST_X and ST_Y ? Steps to reproduce. Table: CREATE TABLE `t_address`...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found