-
Notifications
You must be signed in to change notification settings - Fork 219
Updating entries
Simple.Data method Update is used to create new 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 DELETE 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.
_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>
Sending the whole object to Simple.Data Delete command results in not very efficient OData command because OData adapter receives Delete argument as a Simple.Data SimpleExpression object with all entry properties joined as a search condition. See below how large is a request URI.
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: GET Products(1)
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:
Modifying data
Simple.Data documentation for Update