Search code examples
wcfodatawcf-data-services

WCF OData patching an entity set


Is it possible in update multiple entites based on a filter query in a batch request?

As an example of what I'm trying to achieve, say I want to update all product categories from foo to bar in one request to an OData endpoint, is there something like this that would work:

--batch_36522ad7-fc75-4b56-8c71-56071383e77b
Content-Type: multipart/mixed; boundary=changeset_fa7b-4aa9-a01f

GET /api/products?$filter=category eq 'foo' HTTP/1.1
Accept:application/json
Content-ID: 1

--changeset_fa7b-4aa9-a01f
Content-Type: application/http
Content-Transfer-Encoding: binary

PATCH $1 HTTP/1.1
Accept: application/json
Content-Type: application/json;odata=verbose

{"category":"bar"}

--changeset_fa7b-4aa9-a01f--

--batch_36522ad7-fc75-4b56-8c71-56071383e77b--

Solution

  • I'm afraid the answer is no. There's no support for that in the protocol. And even if you remove the filtering from the question, trying to update all entities in the entity set so that they will have a same new value, the answer is still no.

    You could probably do this yourself. Like,

    Get /service.svc/MyEntitySet
    

    and for every entity you get back, send a PATCH to update it individually.

    In addition, if this kind of operation is going to be done frequently, the service author could write a specific service operation or action to do this on the server side. For example, write something called "ClearAllNames", and a user could invoke that, and the server would go through every entity and clear its name field.