Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship

32,759

Solution 1

OK I figured this one out. As usual it was just me making a mistake and not catching it right away because I was looking in the wrong place.

The issue was due to my table inheritance and the configuration at the database level. I had the child table's (UserAdmin) foreign key field defined as an identity, which then showed up in my model as StoreGeneratedPattern = Identity. This was causing the 'An item with the same key has already been added error' when EF was trying to propagate the parent's primary key to it's child's foreign key field.

Once I removed the identity specification from the UserAdmins table the issue went away.

Problem solved.

Solution 2

Yes that is exactly the problem. How did you get membershipUser entity? If you didn't get it from current ObjectContext EF thinks that it is the new one and it tries to insert it to aspnet_users table.

Try to use this code after adding user to ObjectSet:

// db is ObjectContext instance
db.ObjectStateManager.ChangeObjectState(membershipUser, EntityState.Unchanged);

Therea are probably some other ways how to achieve same result but this works for me.

Share:
32,759
BrooklynDev
Author by

BrooklynDev

Updated on August 30, 2020

Comments

  • BrooklynDev
    BrooklynDev over 3 years

    I have an entity with a foreign key relationship to an asp.net membership provider users table.

    This portion of the model looks like this:

    alt text

    I can't seem to assign the foreign key relationship when inserting the Users table record, the record containing the foreign key to the aspnet_Users table. I keep getting the error:

    An item with the same key has already been added.

    The code I'm using looks like:

    UserAdmin userToAdd = new UserAdmin();
    ...
    userToAdd.aspnet_Users = membershipUser;
    //line above OR line below
    userToAdd.aspnet_UsersReference.EntityKey = new System.Data.EntityKey("ProjectEntities.aspnet_Users", "UserId", membershipUser.UserId);
    
    db.AddToUsers(userToAdd);
    db.SaveChanges();
    

    Is the issue that I'm asking the EF to add a new aspnet_Users table record (the record for the associated primary key table) when that record is already there?

    How would I assign a foreign key value to an entity where the primary key record already exists?

    Updated test code block:

    MembershipUser membershipUser = Membership.CreateUser("[email protected]", "pass", "[email protected]");
    
    Entities db = new Entities();
    UserAdmin newUser = new UserAdmin();
    newUser.Name = "blah";
    newUser.DateAdded = DateTime.Now;
    
    Guid key = new Guid(membershipUser.ProviderUserKey.ToString());
    aspnet_Users membershipUserRecord = db.aspnet_Users.Where(u => u.UserId == key).FirstOrDefault();
    newUser.aspnet_Users = membershipUserRecord;
    //newUser.aspnet_UsersReference.EntityKey = new System.Data.EntityKey("Entities.aspnet_Users", "UserId", membershipUserRecord.UserId);
    db.ObjectStateManager.ChangeObjectState(membershipUserRecord, EntityState.Unchanged);
    
    db.AddToUsers(newUser);
    db.SaveChanges();
    

    This code generates the error:

    An item with the same key has already been added.

  • BrooklynDev
    BrooklynDev over 13 years
    I tried using ChangeObjectState() but to no avail, still getting the 'An item with the same key has already been added' error. I've added my test code looks in the original post.
  • Ladislav Mrnka
    Ladislav Mrnka over 13 years
    I checked your code and it should work. Moreover when you select aspnet user by EF you don't need to set its state - it is already in unchanged state. Is the Users table empty? Is UserId in Users table auto generated? If you use SQL Server and if you have SQL Profiler installed you can check what SQL commands are executed.
  • BrooklynDev
    BrooklynDev over 13 years
    I've run a profile on the database and I don't seem to see any insert statements after the Membership provider does it's thing for Membership.CreateUser(). Calling SaveChanges() doesn't seem to execute any insert or update statements against the database at all.
  • BrooklynDev
    BrooklynDev over 13 years
    It seems as though the error is happening in object collections before changes are pushed to the database: [ArgumentException: An item with the same key has already been added.] System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12753120 System.Data.Mapping.Update.Internal.UpdateCommandOrderer.Add‌​ServerGenDependencie‌​s() +232 [UpdateException: A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.]