Skip to content

Updating entries with links

object edited this page Oct 9, 2012 · 8 revisions

Simple.Data method Update is used to create new 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

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);

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