Adding an entity to the Breeze saveMap which comes from DB (not a totally new entity)

让人想犯罪 __ 提交于 2019-12-11 19:27:05

问题


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

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

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.

I found a way to proceed to add a new entity to the saveMap here: Breeze BeforeSaveEntities: how to modify 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.

I tried like this:

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.

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

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

**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.

Thanks for your help.


UPDATE

As suggested by Jay:

ei.ForceUpdate = true; 

No need to take care of OriginalValuesMap in this case.


回答1:


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

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.



来源:https://stackoverflow.com/questions/18748076/adding-an-entity-to-the-breeze-savemap-which-comes-from-db-not-a-totally-new-en

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!