Skip to content

Updating entries with links

Jason Finch edited this page Aug 23, 2018 · 17 revisions

Update a product and link a category

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var products = await client
    .For("Products")
    .Set(new { ProductName = "Test2", UnitPrice = 18m, CategoryID = 1 })
    .InsertEntryAsync();
await client
    .For("Products")
    .Key(product)
    .Set(new { UnitPrice = 19m, Category = category })
    .UpdateEntryAsync();

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var products = await client
    .For<Products>()
    .Set(new { ProductName = "Test2", UnitPrice = 18m, CategoryID = 1 })
    .InsertEntryAsync();
await client
    .For<Products>()
    .Key(product)
    .Set(new { UnitPrice = 19m, Category = category })
    .UpdateEntryAsync();

Dynamic syntax

var x = ODataDynamic.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test1")
    .InsertEntryAsync();
var products = await client
    .For(x.Products)
    .Set(x.ProductName = "Test2", x.UnitPrice = 18m, x.CategoryID = 1)
    .InsertEntryAsync();
await client
    .For(x.Products)
    .Key(product)
    .Set(x.UnitPrice = 19m, x.Category = category)
    .UpdateEntryAsync();

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.OData.Client detects this condition and issues two separate requests (POST followed by DELETE).

Untyped syntax

var products = await client
    .For("Products")
    .Set(new { ProductName = "Test2", UnitPrice = 18m, CategoryID = 1 })
    .InsertEntryAsync();
await client
    .For("Products")
    .Key(product)
    .Set(new { UnitPrice = 19m, Category = null })
    .UpdateEntryAsync();

Typed syntax

var products = await client
    .For<Products>()
    .Set(new { ProductName = "Test2", UnitPrice = 18m, CategoryID = 1 })
    .InsertEntryAsync();
await client
    .For<Products>()
    .Key(product)
    .Set(new { UnitPrice = 19m, Category = null })
    .UpdateEntryAsync();

Dynamic syntax

var x = ODataDynamic.Expression;
var products = await client
    .For(x.Products)
    .Set(x.ProductName = "Test2", x.UnitPrice = 18m, x.CategoryID = 1)
    .InsertEntryAsync();
await client
    .For(x.Products)
    .Key(product)
    .Set(x.UnitPrice = 19m, x.Category = null)
    .UpdateEntryAsync();

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">
    <br/>
    <title />
    <br/>
    <updated>2012-10-11T17:11:57.3670000Z</updated>
    <br/>
    <author>
        <br/>
        <name />
        <br/>
    </author>
    <br/>
    <id />
    <br/>
    <content type="application/xml">
        <br/>
        <m:properties>
            <br/>
            <d:UnitPrice m:type="Edm.Decimal">19</d:UnitPrice>
            <br/>
        </m:properties>
        <br/>
    </content>
    <br/>
</entry>
<br/>

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


See also:
Updating entries
Linking and unlinking entries
Modifying data