Updating entries

object edited this page Feb 11, 2013 · 13 revisions

Simple.Data method Update is used to update entries in OData collections.

OData supports updates only by resource key, so in case the entry is identified by non-key properties, the adapter first issue HTTP GET request to retrieve entry's key value and then invokes HTTP PUT request with the key value.

OData adapter tries to optimize request payload and wherever possible sends MERGE command with only the changed data instead of PUT with the whole entry content.


Update a single field of a product identified by name

_db.Products.UpdateByProductName(ProductName: "Chai", UnitPrice: 123m);
var product = _db.Products.FindByProductName("Chai");
Assert.Equal(123m, product.UnitPrice);

Request URI: GET Products?$filter=ProductName+eq+%27Chai%27
Request URI: MERGE Products(1)
Request content:

<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title />
  <updated>2012-10-09T12:58:16.7080000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:UnitPrice m:type="Edm.Decimal">123</d:UnitPrice>
    </m:properties>
  </content>
</entry>

Update a product identified by the whole object

var product = _db.Products.Insert(ProductName: "Test2", UnitPrice: 18m);
product = _db.Products.FindByProductName("Test2");
Assert.NotNull(product);
_db.Products.Delete(product);
product = _db.Products.FindByProductName("Test2");
Assert.Null(product);

Request URI: PUT Products(1)
Request content:

<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title />
  <updated>2012-10-09T14:19:08.0280000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:SupplierID m:type="Edm.Int32">1</d:SupplierID>
      <d:CategoryID m:type="Edm.Int32">1</d:CategoryID>
      <d:ProductName>Chai</d:ProductName>
      <d:EnglishName>Dharamsala Tea</d:EnglishName>
      <d:QuantityPerUnit>10 boxes x 20 bags</d:QuantityPerUnit>
      <d:UnitPrice m:type="Edm.Decimal">123</d:UnitPrice>
      <d:UnitsInStock m:type="Edm.Int16">39</d:UnitsInStock>
      <d:UnitsOnOrder m:type="Edm.Int16">0</d:UnitsOnOrder>
      <d:ReorderLevel m:type="Edm.Int16">10</d:ReorderLevel>
      <d:Discontinued m:type="Edm.Boolean">false</d:Discontinued>
    </m:properties>
  </content>
</entry>

See also:
Updating entries with links
Modifying data
Simple.Data documentation for Update