Question

What is use patch in odata? How patch work ?

I find many example for odata but i can't understand patch which situation i can use patch. Below is just example for patch

Partial Updates (PATCH requests)


public HttpResponseMessage PatchProduct(int id, Delta<Product> product)
{
    Product dbProduct = _db.Products.SingleOrDefault(p => p.ID == id);
    if (dbProduct == null)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }

    product.Patch(dbProduct);
    _db.SaveChanges();

    return Request.CreateResponse(HttpStatusCode.NoContent);
}
Was it helpful?

Solution

PUT request have replace semantics which makes updates all or nothing, meaning you have to send all the properties even if only a subset have changed.This is where PATCH comes in, PATCH allows clients to send just the modified properties on the wire, essentially allowing for partial updates.

Given this we simply retrieve the product from the database and then we call Patch to apply the changes requested to the entity in the database. Once that is done we call SaveChanges to push the changes to the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top