Breeze BeforeSaveEntityonly only allows update to Added entities
Asked Answered
H

2

9

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;
        }
...
Hushhush answered 28/2, 2013 at 22:32 Comment(0)
N
9

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.

Neary answered 3/3, 2013 at 17:50 Comment(0)
A
7

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.

Ailanthus answered 28/2, 2013 at 23:0 Comment(6)
Yikes. We are looking into this and doing the more intuitive thing. Standby for news.Katti
Thanks that worked, changed answer to declare entity as dynamic.Hushhush
Yes, thanks for the workaround. But OMG that's awful. I'm looking into a better answer in a not-too-distant release. Thanks for finding this ... and surviving it.Katti
Ward: did anything come of this or is this still the way to update a "CreatedDate" on the server before saving?Ire
@Katti Wondering if this is still the only way to do this. So far I haven't found anything to the contrary.Wilonah
It remains the one and only way. We have no plans to change it. While I was horrified two years ago, I've come to accept it.Katti

© 2022 - 2024 — McMap. All rights reserved.