Breeze BeforeSaveEntityonly only allows update to

2020-03-13 04:09发布

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;
        }
...

标签: breeze
2条回答
不美不萌又怎样
2楼-- · 2020-03-13 04:38

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.

查看更多
女痞
3楼-- · 2020-03-13 04:45

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.

查看更多
登录 后发表回答