在MVC 3实体框架中从多个链接表到多个链接表添加关系的主键冲突 [英] Primary key violation on adding of relationship from many to many linked tables in MVC 3 entity framework

查看:114
本文介绍了在MVC 3实体框架中从多个链接表到多个链接表添加关系的主键冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了数十个问题,这些问题起初似乎有类似的问题,但看起来却不太一样.如果在某个地方回答了这个问题,会道歉,但是就像我说的那样,我已经读了很多书,却找不到答案.

I've read through scores of questions on here that seem at first to have a similar problem but just don't seem quite the same. Massive apologies if this is answered somewhere but like I say I've read through loads and can't find an answer.

我正在使用Entity Framework和MVC 3. 我试图将标签添加到我的实体框架中的产品中,该实体框架具有多对多的关系,并且使用表将它们与链接表中的两个键简单地链接起来,因此EF将标签压缩为Product的属性.表格设置如下:

I'm using Entity Framework and MVC 3. I'm trying to add tags to products in my entity framework, which have a many to many relationship and a table linking them with simply the two keys in the link table, so EF condenses the Tags into a property of Product. Tables are set up like so:

产品:ProductID [int,主键],名称等

Product: ProductID [int, primary key], Name, etc.

标签:TagName [字符串,主键]

Tags: TagName [string, primary key]

ProductTags:产品ID,TagName

ProductTags: ProductID, TagName

因此要访问ProductTag,我只能使用product.Tags

So to access ProductTags I can just use product.Tags

这是我的代码:

dbProduct.Tags.Clear();
foreach (var tag in productModel.Tags)
{
    Data.Tag dbTag = new Data.Tag();
    dbTag.TagName = tag;
    dbProduct.Tags.Add(dbTag);
}

dbProduct是产品实体,而Data是名称空间. productModel.Tags是List<string>

dbProduct being a Product entity, and Data being the namespace. productModel.Tags is a List<string>

当我SaveChanges()时,出现以下异常:

When I SaveChanges() I get the following exception:

违反了PRIMARY KEY约束'PK_Tags_1'.无法插入 \ r \ n对象'dbo.Tags'中的重复键. 终止."

"Violation of PRIMARY KEY constraint 'PK_Tags_1'. Cannot insert duplicate key in object 'dbo.Tags'.\r\nThe statement has been terminated."

那么真正让我着迷的是:为什么要尝试向dbo.Tags添加任何内容?在我看来,这应该只是添加到ProductTags中而不是标签中.我没有在此方法的其他地方提及标签,因此绝对不要尝试将任何内容直接添加到标签"表中.感觉我的EF中可能设置了错误,但我想不出它是由数据库生成的.

So what's really getting me is: why is it trying to add anything to dbo.Tags? It seems to me this should simply add to ProductTags not Tags. I make no mention of tags elsewhere in this method and so at no point try to add anything directly into the Tags table. It feels like I may have something set up wrong in my EF but I can't think what and it was generated from the database.

再次抱歉,如果这很明显,我感到很愚蠢.任何帮助都非常感谢.

Sorry again if this is blindingly obvious, I'm feeling pretty stupid. Any help very much appreciated.

推荐答案

问题是您正在使用现有的主键创建新的Tag对象.调用SaveChanges()时,EF检测到已跟踪的实体的更改,并添加了新实体.由于您的新Tag对象没有被EF跟踪,因此它会尝试将其插入.

The problem is you are creating a new Tag object with an existing primary key. When SaveChanges() is called EF detects the changes of the entities its already tracking and new entities added. Since your new Tag object was not tracked by EF, it tries to insert it.

您需要明确告诉EF创建的标签是现有标签.为此,您需要附加它.

You need to explicitly tell EF that the created tag is an existing one. To do that you need to attach it.

dbProduct.Tags.Clear();
foreach (var tag in productModel.Tags)
{
    Data.Tag dbTag = new Data.Tag();
    dbTag.TagName = tag;

    db.TagSet.Attach(dbTag);

    dbProduct.Tags.Add(dbTag);
}

此代码假定您没有多次附加单个标签,并且所有标签都是现有标签.

This code assumes that you are not attaching single tag multiple times and all tags are existing tags.

这篇关于在MVC 3实体框架中从多个链接表到多个链接表添加关系的主键冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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