简体   繁体   English

“实体类型‘X’的实例映射到具有键值 Guid 的同一行,但具有不同的原始属性”异常是什么意思

[英]What does mean "The instance of entity type 'X' are mapped to the same row with the key value Guid, but have different original property" exception

I have a complex model in ef core.我在 ef 核心中有一个复杂的 model。 There are a lots of inheritance and references.有很多 inheritance 和参考资料。 Sometimes I am getting an error on saving context:有时我在保存上下文时遇到错误:

The instance of entity type 'X' and the instance of entity type 'Y' are mapped to the same row with the >key value '{Guid: 29421520-1733-4659-b38b-f1b8bdafb0c1}', but have different original property values实体类型“X”的实例和实体类型“Y”的实例映射到具有>键值“{Guid: 29421520-1733-4659-b38b-f1b8bdafb0c1}”的同一行,但具有不同的原始属性值

What does this error mean?这个错误是什么意思? Is the problem is in the model, or in business logic, mapping... and how to resolve it?问题出在model,还是出在业务逻辑、映射……如何解决?

You never got an answer eh?你从来没有得到答案是吗? I'm assuming you found an answer by now, but I just ran into this problem today, and since I resolved it, figured I'd share my answer.我假设你现在已经找到了答案,但我今天刚遇到这个问题,既然我解决了它,我想我会分享我的答案。

Cause : two separate entities that were both mapped to the same column in the database and were both being saved during the same transaction but with different values.原因:两个独立的实体都映射到数据库中的同一列,并且都在同一事务中保存但具有不同的值。

In my case, I had a DateTime column Xyz in a ProgressTracking [ComplexType] child-object, and apparently someone couldn't find the property in the ComplexType object so they added the same fields to the parent object...so reading works fine and most saves work fine...until you update one of the values but not the other...then when you save...CRASH, If you wanted it to work: I'm assuming you would have to do:在我的例子中,我在 ProgressTracking [ComplexType] 子对象中有一个 DateTime 列 Xyz,显然有人无法在 ComplexType object 中找到该属性,因此他们将相同的字段添加到父对象 object...所以阅读工作正常大多数保存工作正常......直到你更新其中一个值而不是另一个......然后当你保存......崩溃时,如果你想让它工作:我假设你必须这样做:

var newValue = DateTime.Now;
obj.Xyz = newValue;
obj.ProgressTracking.Xyz = newValue;

Which, I'm not going to try the above to see if it works because that would be asinine.其中,我不打算尝试上面的方法来查看它是否有效,因为那将是愚蠢的。 So:所以:

Solution : remove the property from the parent object and leave it on the ComplexType, then refactor any usages...there were none:-/... I really hope I'm not the one who did that (afraid to check source control:-P )解决方案:从父 object 中删除属性并将其保留在 ComplexType 上,然后重构任何用法...没有:-/...我真的希望我不是这样做的人(害怕检查源代码控制:-P)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF:无法跟踪实体类型X的实例,因为已经跟踪了具有相同密钥的此类型的另一个实例 - EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked 获取'实体类型的实例'X''例外 - Getting 'The instance of entity type 'X'' Exception Entity Framework Core:无法跟踪实体类型的实例,因为另一个实例具有相同的键值 - Entity Framework Core : instance of entity type cannot be tracked because another instance with same key value Entity framework core 无法跟踪实体类型的实例,因为另一个实例具有相同的键值 - Entity framework core The instance of entity type cannot be tracked because another instance with the same key value 无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 附加类型为“X”的实体失败,因为相同类型的另一个实体已具有相同的主键值 - Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value 实体框架:无法跟踪类型 x 的实体,因为已跟踪具有相同键的另一个实例 - Entity Framework: Entity of type x cannot be tracked because another instance with same key is already being tracked 实体框架实体不同的键映射到同一行 - Entity Framework entities different keys are mapped to same row Entity Framework 如何为主键值生成 GUID? - How does Entity Framework generate a GUID for a primary key value? 错误:无法跟踪实体类型“X”的实例,因为已在跟踪另一个具有键值“{ID:3}”的实例 - Error: The instance of entity type 'X' cannot be tracked because another instance with the key value '{ID: 3}' is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM