我有一个场景,我知道实体的主键(从不相关的源检索),并且我只想更新 1 个属性(数据库列)。我尚未从数据库中检索实体。如果可能的话,我希望不必进行额外的往返。
我使用 manager.createEntity 创建实体。 我更新了其中一项属性。 然后将entityAspect设置为setModified(); 保存更改时,所有未更新的属性都将设置为其默认值,并且生成的 SQL UPDATE 语句会尝试更新所有映射列。
有没有办法告诉breeze只为特定的属性/列生成SQL?
谢谢
最佳答案
正如您所发现的,originalValuesMap
的属性在 Breeze 服务器的 ContextProvider
准备保存请求时指导它。这记录在 ContextProvider topic 中.
在您的示例中,您在更改属性后调用setModified
。所做的只是更改 EntityState;它不会在客户端实体的 entityAspect.originalValuesMap
中创建条目...因此发送到服务器的 originalValuesMap
为空。
I'm a little surprised that the
EFContextProvider.SaveChanges
prepared an EF update of the entire entity. I would have guessed that it simply ignored the entity all together. I'm making a mental note to investigate that myself. Not saying the behavior is "right" or "wrong".
您不必操纵originalValuesMap
来实现您的目标。只需改变顺序即可。试试这个:
var foo = manager.createEntity('Foo', {
id = targetId
}, breeze.EntityState.Unchanged); // create as if freshly queried
foo.bar = 'new value'; // also sets 'originalValues' and changes the EntityState
manager.saveChanges(); // etc.
请告诉我们这是否有效。
关于save - 使用breeze.js我只想发送非缓存实体的更新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28816493/