Bernardo Pacheco
Bernardo Pacheco

Reputation: 1445

Breeze.js - Changing a property value of an entity on the server before saving the entity

My question is:

Is it possible, in Breeze.js, to change/set the property value of an entity on the server before saving this entity?

For example, assuming that there is an entity called Product and this entity has a property called Price, I want, in the server and before saving the entity, multiply the Price value by a constant.

Looking here and here, I tried using the following methods: BeforeSaveEntity(entityInfo), BeforeSaveEntities(saveMap), SaveChangesCore(saveMap).

In the source code , I understood that BeforeSaveEntity(entityInfo) and BeforeSaveEntities(saveMap) are only used for validating an entity. It's not what I'm looking for.

In the method SaveChangesCore(saveMap) with source code described here, I thought that here was the place to change the property value of the entity.

So I tried the following, but it didn't work. The value was not updated in the database

protected override List<KeyMapping> SaveChangesCore(Dictionary<Type, List<EntityInfo>> saveMap)
        {
            foreach (var entity in saveMap[typeof(Product)])
            {
                var product = (Product)entity.Entity;

                product.Price =  product.Price * 10; // changing the value of the property    

            }

            return base.SaveChangesCore(saveMap);    
        }

Thanks in advance,

Bernardo Pacheco

Upvotes: 2

Views: 1998

Answers (2)

Jay Traband
Jay Traband

Reputation: 17052

We have similar tests that do exactly this so I am not sure what is happening, but I do have a couple of suggestions. First change

foreach (var entity in saveMap[typeof(Product)]) {
   ... 
}

to

List<EntityInfo> entities;
if (saveMap.TryGetValue(typeof(Product), out entities)) {
   foreach (var entity in entities) { 
      ...
   }
}

The reason for this is that saveMap is a .NET Dictionary, which will throw an KeyNotFoundException when using the indexer ( saveMap[typeof(Product)]) if the key is not found. And there will be many cases when a saveMap does not include a particular entityType.

Also, and this is probably just a typo but the return type from your overridden method should be a Dictionary< Type, List< EntityInfo>> and not a List< EntityInfo>.

I would also step into your code and make sure that it is executing the way you expect. It is possible that your code is throwing an exception and it is getting eaten before propagating out to your UI.

Upvotes: 2

Juli&#225;n Yuste
Juli&#225;n Yuste

Reputation: 1472

You can do it at your dbcontext class. You can override the SaveChanges method and do something like:

public override int SaveChanges() {
    foreach ( var entry in this.ChangeTracker.Entries()
            .Where( e => e.State ==EntityState.Added ) ){
        var entity=entry.Entity as Product;
        if(entity!=null){
            entity.Price =  entity.Price * 10;
        }
    }
    return base.SaveChanges();
}

I'm assuming that you are using entity framework.

I think that this post is about your problem also:

Breeze BeforeSaveEntityonly only allows update to Added entities

Upvotes: 3

Related Questions