user2363071
user2363071

Reputation: 249

Client OData Patch without retrieving object first?

I am using a C# OData 4 client as described here:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app

I have a product class and it has an Id, Name, Price and Category. I'd like to do something like:

        var product = new ProductService.Models.Product { 
          Id = 2,
          Price = 4
        };

        container.AttachTo("Products", product);
        container.UpdateObject(product);

So that I can update only the price property and ignore all the rest of them. I can see that this won't work because Name and Category are Created as null when Product object is created so they will be sent in the resulting request as null.

Is there a way of doing this without first retrieving the object that I want to update? (I'm guessing that I need to go down the HttpClient route).

Upvotes: 2

Views: 1127

Answers (1)

Tan Jinfu
Tan Jinfu

Reputation: 3347

One workaround is to use HttpClient directly:

        HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);
        request.Content = new StringContent(@"{{""@odata.type"":""#ProductService.Models.Product"",""Price"":3000}}");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
        HttpResponseMessage  response = new HttpClient.SendAsync(request).Result;

Upvotes: 2

Related Questions