[英]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.