Search code examples
saveentity-framework-6breezeentities

Using breeze.js I only want to send updated properties for a non-cached entity


I have a scenario where I know the primary key of an entity (retrieved from an unrelated source), and I want to update just 1 property (db column). I have NOT already retrieved the entity from the database. If possible I would like to not have to make this extra round trip.

I create the entity using manager.createEntity. I update one of the properties. Then set the entityAspect to setModified(); When saving changes, all the properties that were not updated are set to their default values, and the generated SQL UPDATE statement attempts to update all mapped columns.

Is there a way to tell breeze to only generate SQL for specific properties/columns?

thanks


Solution

  • As you discovered, the properties of the originalValuesMap guide the Breeze server's ContextProvider as it prepares the save request. This is documented in the ContextProvider topic.

    In your example, you call setModified after you've changed the property. All that does is change the EntityState; it doesn't create an entry in the client entity's entityAspect.originalValuesMap ... therefore the originalValuesMap sent to the server is empty.

    I'm a little surprised that the EFContextProvider.SaveChanges prepared an EF update of the entire entity. I would have guessed that it simply ignored the entity all together. I'm making a mental note to investigate that myself. Not saying the behavior is "right" or "wrong".

    You do not have to manipulate the originalValuesMap to achieve your goal. Just change the sequence. Try this:

    var foo = manager.createEntity('Foo', {
            id = targetId
        }, breeze.EntityState.Unchanged);  // create as if freshly queried
    
    foo.bar = 'new value';  // also sets 'originalValues' and changes the EntityState
    
    manager.saveChanges(); // etc.
    

    Let us know if that does the trick.