Skip to content

Updating entries with links

object edited this page Feb 12, 2013 · 8 revisions

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


Update a product and link a category

var category = _db.Categories.Insert(CategoryName: "Test1");
var product = _db.Products.Insert(ProductName: "Test2", UnitPrice: 18m, CategoryID: 1);
_db.Products.UpdateByProductName(ProductName: "Test2", UnitPrice: 19m, Category: category);
product = _db.Products.FindByProductName("Test2");
Assert.Equal(19m, product.UnitPrice);
Assert.Equal(category.CategoryID, product.CategoryID);

Request URI: MERGE Products(78)
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:49:36.0090000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:UnitPrice m:type="Edm.Decimal">19</d:UnitPrice>
    </m:properties>
  </content>
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Category" type="application/atom+xml;type=Entry" title="Category" href="Categories(9)" />
</entry>

Update a product and unlink a category

At the present time OData protocol does not support updating entry properties and clearing it's links in a single HTTP request (it is however possible to set links within a single HTTP request as shown in the first example). But Simple.Data OData adapter detects this condition and issues two separate requests (POST followed by DELETE).

var category = _db.Categories.Insert(CategoryName: "Test1");
var product = _db.Products.Insert(ProductName: "Test2", UnitPrice: 18m, CategoryID: 1);
_db.Products.UpdateByProductName(ProductName: "Test2", UnitPrice: 19m, Category: null);
product = _db.Products.FindByProductName("Test2");
Assert.Equal(19m, product.UnitPrice);
Assert.Null(product.CategoryID);

Request URI: MERGE Products(78)
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-11T17:11:57.3670000Z</updated>
<author>
<name />
</author>
<id />
<content type="application/xml">
<m:properties>
<d:UnitPrice m:type="Edm.Decimal">19</d:UnitPrice>
</m:properties>
</content>
</entry>

Request URI: DELETE Products(78)/$links/Category


See also:
Updating entries
Linking and unlinking entries
Modifying data
Simple.Data documentation for Update