LINQ to SQL ForeignKeyReferenceAlreadyHasValueException 错误
- 作者: 花果山梁山泊荆州牧红孩儿
- 来源: 51数据库
- 2022-12-13
问题描述
当我尝试更改外键时会生成此错误.我知道这是一个非常常见的错误,我找到了大量关于它的信息并尝试实施我发现的修复程序,但在尝试更新密钥时仍然出现此错误.参考线程>
最初我只是直接赋值,而不是尝试映射实体.
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);
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
