Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 388
BrooklynDev Avatar asked Aug 29 '10 09:08

BrooklynDev


2 Answers

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.

like image 113
BrooklynDev Avatar answered Oct 12 '22 07:10

BrooklynDev


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.

like image 26
Ladislav Mrnka Avatar answered Oct 12 '22 07:10

Ladislav Mrnka