问题描述
我有以下数据模型:
我的业务逻辑适用于分离的实体,所以我使用GraphDiff执行更新。我无法更新PerfModes / CalcPoints关联。在概念上,Block拥有CalcPoints和PerfModes,但CalcPoints可以与任意数量的PerfModes相关联。
My business logic works with detached entities so I'm using GraphDiff to perform updates. I'm having trouble updating the PerfModes/CalcPoints association. Conceptually, Block owns CalcPoints and PerfModes, but CalcPoints can be associated with any number of PerfModes.
我正在Block级别进行更新。我想出的代码不会抛出任何错误(而其他尝试),但也不更新PerfModes / CalcPoints关联。
I'm trying to do updates at the Block level. The code I came up with doesn't throw any errors (while other attempts did) but neither does it update the PerfModes/CalcPoints association.
container.UpdateGraph(block, map => map
.OwnedCollection(b => b.HistPoints)
.OwnedCollection(b => b.CalcPoints)
.OwnedCollection(b => b.PerfModes, with => with
.OwnedCollection(p => p.FilterCriterion, with2 => with2
.OwnedCollection(fc => fc.Filters, with3 => with3
.AssociatedEntity(f => f.OperatorType)
.AssociatedEntity(f => f.CalcPoint))))
.AssociatedCollection(p => p.CalcPoints)
);
我可能没有完全掌握EF图形和GraphDiff。如何确保多对多PerfModes / CalcPoints关联正确更新?
I probably don't have a full grasp of EF graphs and GraphDiff. How do I ensure that the many-to-many PerfModes/CalcPoints association gets updated correctly?
编辑
在查看了andyp的答案后,我从GitHub中下载了最新版本的GraphDiff,并尝试了以下映射:
After looking over andyp's answer, I pulled down the latest version of GraphDiff from GitHub and tried the following mappings:
container.UpdateGraph(block, map => map
.OwnedCollection(b => b.CalcPoints)
.OwnedCollection(b => b.PerfModes,
with => with.AssociatedCollection(pm => pm.CalcPoints)));
这正确地更新了我的PerfModes / CalcPoints关联。我切换到我原来的映射,仍然看到协会没有更新的问题,所以试图一次更新整个模型似乎有问题。我可以做多个UpdateGraph电话,但是最好的方法是打破它们。
This correctly updates my PerfModes/CalcPoints association. I switched back to my original mappings and still saw the issue of the association not updating, so it seems there's a problem with trying to update the entire model at once. I'd be fine with making multiple UpdateGraph calls, but what would be the best way to break them out?
获取当前来源,并构建它你自己。
DbContext
在调用 UpdateGraph()
之前,您希望GraphDiff所做的更改以及它无法创建的更改。 - Is there anything (significant) you did differently then me above?
- Do you call
SaveChanges()
afterUpdateGraph()
? It's not implied! - Do you have the latest version of GraphDiff? Please note, that the NuGet package is quite outdated, it's better to grab the current source from Github and build it yourself.
- If your problem persists please update your question with the exact scenario that isn't working: please include the state of your
DbContext
before callingUpdateGraph()
, the changes you expect GraphDiff to make and the ones it fails to make.
编辑:
您的映射不正确, code> Block.CalcPoints 两次,一次作为拥有的集合(首先调用 OwnedCollection(..)
),一次作为关联集合(最后只调用 AssociatedCollection(..)
)。所以你从来没有告诉GraphDiff来映射 PerfModes.CalcPoints
,反过来也不会更新该集合..; - )
EDIT:Your mapping wasn't correct after all, you're mapping Block.CalcPoints
twice, once as an owned collection (first call to OwnedCollection(..)
) and once as an associated collection (last and only call to AssociatedCollection(..)
). So you never told GraphDiff to map PerfModes.CalcPoints
and it, in turn, never updates that collection.. ;-)
要求GraphDiff这样做,请从最后一行之前的行尾移动到最后一行末尾的一个)
,你应该很好你的缩进与括号相匹配)。正确的映射看起来像这样(最后一行末尾有两个括号):
To ask GraphDiff to do that please move one )
from the end of the line before the last line to the end of the last line and you should be fine (after that your indentation matches the brackets). The correct mapping looks like this (two closing brackets at the end of the last line):
container.UpdateGraph(block, map => map
.OwnedCollection(b => b.HistPoints)
.OwnedCollection(b => b.CalcPoints)
.OwnedCollection(b => b.PerfModes, with => with
.OwnedCollection(p => p.FilterCriterion, with2 => with2
.OwnedCollection(fc => fc.Filters, with3 => with3
.AssociatedEntity(f => f.OperatorType)
.AssociatedEntity(f => f.CalcPoint)))
.AssociatedCollection(p => p.CalcPoints))
);
这篇关于使用GraphDiff更新多对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!