Breeze BeforeSaveEntityonly only allows update to Added entities

筅森魡賤 提交于 2019-12-19 05:43:48

问题


Don't know if this is intended or a bug, but the following code below using BeforeSaveEntity will only modify the entity for newly created records (EntityState = Added), and won't work for modified, is this correct?

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        var entity = entityInfo.Entity;
        if (entity is User)
        {
            var user = entity as User;
            user.ModifiedDate = DateTime.Now;
            user.ModifiedBy = 1;
        }
...

回答1:


The root of this issue is that on the breeze server we don’t have any built in change tracking mechanism for changes made on the server. Server entities can be pure poco. The breeze client has a rich change tracking capability for any client side changes but when you get to the server you need to manage this yourself.

The problem occurs because of an optimization we perform on the server so that we only update those properties that are changed. i.e. so that any SQL update statements are only made to the changed columns. Obviously this isn’t a problem for Adds or Deletes or those cases where we update a column that was already updated on the client. But if you update a field on the server that was not updated on the client then breeze doesn't know anything about it.

In theory we could snapshot each entity coming into the server and then iterate over every field on the entity to determine if any changes were made during save interception but we really hate the perf implications especially since this case will rarely occur.

So the suggestion made in another answer here to update the server side OriginalValuesMap is correct and will do exactly what you need.

In addition, as of version 1.1.3, there is an additional EntityInfo.ForceUpdate flag that you can set that will tell breeze to update every column in the specified entity. This isn't quite as performant as the suggestion above, but it is simpler, and the effects will be the same in either case.

Hope this helps.




回答2:


I had the same problem, and I solved that doing this:

protected override bool BeforeSaveEntity(EntityInfo entityInfo)
{
  if(entityInfo.EntityState== EntityState.Modified)
  {
     var entity = entityInfo.Entity;
     entityInfo.OriginalValuesMap.Add("ModificationDate", entity.ModificationDate);
     entity.ModificationDate = DateTime.Now;
  }
}

I think you can apply this easily to your case.



来源:https://stackoverflow.com/questions/15146964/breeze-beforesaveentityonly-only-allows-update-to-added-entities

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