Entity Framework - an element with the same key has already been added. - Error trying to determine foreign key relationship

I have an entity with a foreign key relation to the asp.net provider user table.

This part of the model is as follows:

alt text

I cannot assign a foreign key relationship when I insert a record from the Users table, a record containing the foreign key, into the aspnet_Users table. I keep getting the error:

An item with the same key has already been added.

The code I use looks like this:

 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 problem that I ask EF to add a new aspnet_Users table aspnet_Users (an entry for the related primary key table) when this entry already exists?

How to assign a foreign key value to an object in which a primary key record already exists?

Updated test code block:

 MembershipUser membershipUser = Membership.CreateUser("blah@blah.com", "pass", "blah@blah.com"); 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 an error:

An item with the same key has already been added.

+7
linq linq-to-entities entity-framework entity-framework-4
source share
2 answers

OK, I get it. As usual, I just made a mistake and did not catch him right away, because I was looking for the wrong place.

The problem arose due to my table inheritance and configuration at the database level. I had a child table foreign key field (UserAdmin) defined as an identifier, which then appeared in my model as StoreGeneratedPattern = Identity. This led to an โ€œitem with the same key already being added by mistakeโ€ when EF tried to propagate the parent primary key in the child foreign key field.

After removing the authentication specification from the UserAdmins table, the problem disappeared.

The problem is resolved.

+8
source share

Yes, this is definitely a problem. How did you get unionUser membership? If you did not get it from the current ObjectContext, EF thinks it is new and it tries to insert it into the aspnet_users table.

Try using this code after adding the user to the ObjectSet:

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

There are probably some other ways to achieve the same result, but this works for me.

+2
source share

All Articles