Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breeze BeforeSaveEntityonly only allows update to Added entities

Tags:

breeze

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;
        }
...
like image 548
Chris Small Avatar asked Feb 28 '13 22:02

Chris Small


2 Answers

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.

like image 83
Jay Traband Avatar answered Nov 12 '22 11:11

Jay Traband


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.

like image 24
jvrdelafuente Avatar answered Nov 12 '22 11:11

jvrdelafuente