实体框架-具有相同键的项已添加. -尝试定义外键关系时出错 [英] Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship

查看:339
本文介绍了实体框架-具有相同键的项已添加. -尝试定义外键关系时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与asp.net成员资格提供者用户表具有外键关系的实体.

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:

当插入Users表记录时,我似乎无法分配外键关系,该记录包含aspnet_Users表的外键.我不断收到错误消息:

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();

问题是我要EF在该记录已经存在的情况下添加新的aspnet_Users表记录(关联的主键表的记录)吗?

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?

更新的测试代码块:

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();

此代码生成错误:

已经添加了具有相同键的项目.

An item with the same key has already been added.

推荐答案

好,我知道了这一点.像往常一样,只是我犯了一个错误而没有立即抓住它,因为我在错误的地方寻找.

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.

该问题归因于我的表继承和数据库级别的配置.我将子表的(UserAdmin)外键字段定义为身份,然后在我的模型中显示为StoreGeneratedPattern = Identity.当EF尝试将父级的主键传播到其子级的外键字段时,这将导致具有相同键的项已被添加错误".

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.

一旦我从UserAdmins表中删除了身份规范,问题就消失了.

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

问题解决了.

这篇关于实体框架-具有相同键的项已添加. -尝试定义外键关系时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆