LINQ to SQL的错误ForeignKeyReferenceAlreadyHasValueException [英] LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error
问题描述
当我试图改变一个外键正在生成此错误。
我知道这是一个非常常见的错误,我发现吨关于它的信息,并试图实现我找到了修复,但尝试更新密钥时,我仍然得到这个错误。 参考螺纹
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?
推荐答案
我想你想指定一个ID,当你需要分配的实体。 (我不是100%肯定这是你在做什么)
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屋!