向来自数据库的Breeze saveMap添加一个实体(不是全新的实体) [英] Adding an entity to the Breeze saveMap which comes from DB (not a totally new entity)

查看:89
本文介绍了向来自数据库的Breeze saveMap添加一个实体(不是全新的实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在带有Durandal SPA模板的asp.net应用程序中使用Breeze。

I use Breeze in my asp.net application with the Durandal SPA template.

我需要向数据库中已经存在的saveMap添加一个实体。

I need to add an entity to the saveMap which already exists in DB.

让我们举一个简单的例子:页面显示发票和发票行。用户添加新发票的行,然后点击保存按钮。仅使用修改后的发票行触发SaveChanges控制器的操作。服务器端,将重新计算总数,并且必须修改发票的总数。但是此总计位于发票实体上,因此我们需要将发票的实体添加到saveMap。

Let's take this simple example: the page display an invoice and the invoice's lines. The user add a new invoice's line and click the save button. The SaveChanges controller's action is triggered with ONLY the modified invoice's line. Server side, the total is recalculated and the invoice's total must be modified. But this total is located on the invoice entity so we need to add the invoice's entity to the saveMap.

我在这里找到了一种向saveMap添加新实体的方法:

I found a way to proceed to add a new entity to the saveMap here: Breeze BeforeSaveEntities: how to modify savemap

但是建议的解决方案用于在其中添加新实体saveMap(添加状态)将在数据库上创建新记录。这不是我所需要的。我需要向saveMap添加一个新实体(处于修改状态),该实体将从数据库中获取数据。

But the suggested solution is used to add a new entity to the saveMap (Added state) which will create a new record on DB. This is not what I need. I need to add a new entity to the saveMap which (Modified state) will be get the data from DB.

我尝试过这样:

int invoiceId = 1234;
dc.Configuration.ProxyCreationEnabled = false; // don't forget this!
EFContextProvider<BreezeContext> cp = new EFContextProvider<BreezeContext>();
var acc = dc.Invoices.Where(x => x.Id == invoiceId).FirstOrDefault();
ei = cp.CreateEntityInfo(acc, Breeze.WebApi.EntityState.Modified);
invoices = new List<EntityInfo>();
saveMap.Add(typeof(Invoice), invoices);
invoices.Add(ei);

到目前为止一切都很好。

So far so good.

然后我需要将总属性添加到OriginalValuesMap中(否则修改将不会更新):

Then I need to add the total property to the OriginalValuesMap (otherwise modification will not be updated):

ei.OriginalValuesMap.Add("TotalExclVAT", invoice.TotalExclVAT);

**这不起作用: ei.OriginalValuesMap 为空,因此我无法在其中添加新密钥。

**This don't work: ei.OriginalValuesMap is null and so I cannot add a new key inside.

我不知道这是否是正确的操作方法。希望我的解释足够清楚。

I don't know if this is the right way to proceed. Hope my explanations are clear enough.

谢谢您的帮助。

更新

如杰伊建议的那样:

ei.ForceUpdate = true; 

无需照顾 OriginalValuesMap

推荐答案

我还没有机会深入探讨这个问题,但是您是否看过EntityInfo。 ForceUpdate属性。

I haven't yet had a chance to dig into this yet, but have you looked at the EntityInfo.ForceUpdate property.

当对现有实体进行服务器端修改后,此属性可用于强制整个实体的服务器端更新。可以用来代替显式更新EntityInfo.OriginalValuesMap。

This property may be used to force a server side update of an entire entity when server side modification has been made to an existing entity. May be used in place of explicitly updating the EntityInfo.OriginalValuesMap.

到目前为止,我们仅在发行说明中对此进行了记录,因此可以理解如何错过它。

So far we've only documented this in the release notes so it's understandable how it might be missed.

这篇关于向来自数据库的Breeze saveMap添加一个实体(不是全新的实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆