Search code examples
asp.net-web-apiodatajaydataasp.net-web-api-odata

JayData incorrect behaviour with WebApi v2 Odata during entity update the Patch endpoint get invoked


While trying to update the entity, JayData triggers the PatchEntity method on the WepAPI backend. I find this as an invalid behaviour as for the UpdateEntity should be invoked.

The add and delete entity functionality works OK. On the backend I have a controller that inherits from EntitySetController<>

public class BaseODataController<TService, TEntity, TEntityDto, TIdentityType> : EntitySetController<TEntityDto, TIdentityType>
{
  //.....

        protected override TEntityDto UpdateEntity(TIdentityType key, TEntityDto update)
        {
            // is not getting called
            _service.Update(update);
            return base.UpdateEntity(key, update);
        }

        protected override TEntityDto PatchEntity(TIdentityType key, Delta<TEntityDto> patch)
        {
            // gets called
            return base.PatchEntity(key, patch);
        }


  //.....
}

Here is the code that gets called on the clientside:

 vm.updateRole = function(r) {
        return $data.initService('/odata/$metadata').then(function (context) {
            r.Name = "NewUpdateRole";
            context.Role.update(r);
            r.entityState = $data.EntityState.Modified;
            context.saveChanges().then(function(result) {
                debugger;
            });

        });

Am I missing something here?


Solution

  • JayData sends the MERGE or PATCH requests based on the dataServiceVersion property of the odata provider configuration.

    $data.initService('/odata/$metadata', {dataServiceVersion: '3.0'})
    

    2.0 causes MERGE and 3.0 causes PATCH requests to align with the WCF Data Services OData implementation.

    If this behavior does not meet the WebAPI OData requirements, there is a second customization option to determine the kind of the request:

    $data.initService('/odata/$metadata', {UpdateMethod: 'PATCH'})
    

    You can make a try to modify the PATCH to UPDATE HTTP Verb