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 错误
基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
