LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error(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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误


基础教程推荐
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 创建属性设置器委托 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01