LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误 [英] LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error

查看:13
本文介绍了LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试更改外键时会生成此错误.我知道这是一个非常常见的错误,我找到了大量关于它的信息并尝试实施我发现的修复程序,但在尝试更新密钥时仍然出现此错误.参考线程

最初我只是直接赋值,而不是尝试映射实体.

ticket.assigned_to_group =assigned_to

从那以后我改变了尝试映射我认为正确答案的实体;但是我仍然收到错误.

ticket.assigned_to_group = db.sub_units.Single(f => f.id ==assigned_to).id;

知道为什么这行不通.另外,如果我有一个包含多个外键的表,我是否真的需要对每个键进行新的查询或者有更好的方法吗?

解决方案

我认为当您需要分配实体时,您正在尝试分配 ID.(我不是 100% 确定这是你在做什么)

//不要只分配idPerson.ParentId = parentId;//分配实体Person.Parent = db.Parents.Single(x.Id == parentId);

This error is being generated when I try to change a foreign key. I know this is a very common error I’ve found tons of information about it and tried to implement the fixes I’ve found but I still get this error when trying to update Keys. Reference Thread

Originally I was just directly assigning the value and not trying to map the entities.

ticket.assigned_to_group = assigned_to

I’ve since changed to try and map the entities which I believe the correct answer; however I still get the error.

ticket.assigned_to_group = db.sub_units.Single(f => f.id == assigned_to).id;

Any idea why this would not work. Also if I have a table with multiple foreign keys, do I really need to do a new query for each key or is there a better way?

解决方案

I think you are trying to assign an ID when you need to assign the entity. (I'm not 100% sure this is what you are doing)

// Don't assign just the id
Person.ParentId = parentId;

// Assign the entity
Person.Parent = db.Parents.Single(x.Id == parentId);

这篇关于LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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