efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Unhelpful error message: System.ArgumentException: An item with the same key has already been added.

Open esargent opened this issue 10 years ago • 4 comments

When I ran "dnx ef migrations add initial" I got this error:

System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate(CreateTableOperation operation, IndentedStringBuilder builder)
   at CallSite.Target(Closure , CallSite , CSharpMigrationOperationGenerator , Object , IndentedStringBuilder )
   at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList`1 operations, IndentedStringBuilder builder)
   at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList`1 upOperations, IReadOnlyList`1 downOperations)
   at Microsoft.Data.Entity.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
   at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.<AddMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action)
An item with the same key has already been added.

It would be great if the message could include either the "item" or the "key" or something to help point me to the right answer faster. Maybe the generator could do a check before the Add() and then return an error from there rather than just relying on the basic Dictionary itself?

I do understand what the message is caused by generally, but trying to pinpoint which mapping it is hung up on is where it takes some sleuthing. I imagine new EF users would be pretty frustrated by this one too.

esargent avatar Jan 05 '16 18:01 esargent

@esargent do you have a model you can share so that we can reproduce the issue. Agreed that this is not a helpful error.

rowanmiller avatar Jan 06 '16 22:01 rowanmiller

@rowanmiller this was with RC1-final. I've upgraded to RC2 and haven't been able to repo - however I did just add (https://github.com/aspnet/EntityFramework/issues/4251), which I found while trying to create the repo. It may indicate that error checking changed and while this issue doesn't happen in rc2 it lets in a different problem.

Have to admit that because of this and a few other challenges I've had I decided to just leave ApplicationUser alone and use claims to reference the user-type-specific properties for each user. And the different user types no longer derive from ApplicationUser. Maybe that's a better design anyway - I've been writing internal enterprise apps leveraging AD and roles for so long, I'm still trying to figure out best way to go about this scenario. Might be a good "best practices" document down the road to decide which way to go.

repo for RC1 in next post...

esargent avatar Jan 07 '16 23:01 esargent

here is repo code for RC1

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;


namespace WebApplication5
{


    public class ApplicationUser : IdentityUser
    {
    }


    public class SpecialUser1 : ApplicationUser
    {
        //same property in SpecialUser1 and SpecialUser2
        public string Sex { get; set; }
    }


    public class SpecialUser2 : ApplicationUser
    {
        public string SomethingElse { get; set; }
        public string Sex { get; set; } //same property NAME in SpecialUser1 and SpecialUser2
        public string SomethingElse2 { get; set; }
    }


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }

        //two derived classes with same property NAME as each other, but different types
        public DbSet<SpecialUser1> Users1 { get; set; }
        public DbSet<SpecialUser2> Users2 { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
}

C:\Users\esargent\Documents\Visual Studio 2015\Projects\WebApplication5\src\WebApplication5>dnvm use 1.0.0-rc1-final Adding C:\Users\esargent.dnx\runtimes\dnx-clr-win-x86.1.0.0-rc1-final\bin to process PATH

C:\Users\esargent\Documents\Visual Studio 2015\Projects\WebApplication5\src\WebApplication5>dnx ef migrations add one System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate(CreateTableOperation operation, IndentedStringBuilder builder) at CallSite.Target(Closure , CallSite , CSharpMigrationOperationGenerator , Object , IndentedStringBuilder ) at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate(String builderName, IReadOnlyList1 operations, IndentedStringBuilder builder) at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration(String migrationNamespace, String migrationName, IReadOnlyList1 upOperations, IReadOnlyList1 downOperations) at Microsoft.Data.Entity.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace) at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.b__0() at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action) An item with the same key has already been added.

esargent avatar Jan 07 '16 23:01 esargent

@esargent thanks for confirming that it is fixed. I'll close this one out now.

rowanmiller avatar Jan 07 '16 23:01 rowanmiller