Skip to content

Adding entries with links

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

Insert a product with associated category specified by CategoryID

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test3" })
    .InsertEntryAsync();
var product = await client
    .For("Products")
    .Set(new { ProductName = "Test4", UnitPrice =18m, CategoryID = category["CategoryID] })
    .InsertEntryAsync();
Assert.Equal("Test4", product["ProductName"]);
Assert.Equal(category["CategoryID"], product["CategoryID"]);

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test3" })
    .InsertEntryAsync();
var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test4", UnitPrice = 18m, CategoryID = category.CategoryID })
    .InsertEntryAsync();
Assert.Equal("Test4", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);

Dynamic syntax

var x = ODataDynamic.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test3")
    .InsertEntryAsync();
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test4", x.UnitPrice =18m, x.CategoryID = category.CategoryID)
    .InsertEntryAsync();
Assert.Equal("Test4", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);

Request URI: POST Products
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-08T14:41:14.7420000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test4</d:ProductName>
      <d:UnitPrice m:type="Edm.Decimal">18</d:UnitPrice>
      <d:CategoryID m:type="Edm.Int32">10</d:CategoryID>
    </m:properties>
  </content>
</entry>

Insert a product with associated category specified by category object

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test5" })
    .InsertEntryAsync();
var product = await client
    .For("Products")
    .Set(new { ProductName = "Test6", UnitPrice = 18m, Category = category })
    .InsertEntryAsync();
Assert.Equal("Test6", product["ProductName"]);
Assert.Equal(category.CategoryID, product.CategoryID);

Typed syntax

var category = client
    .For<Categories>()
    .Set(new { CategoryName = "Test5" })
    .InsertEntry();
var product = client
    .For<Products>()
    .Set(new { ProductName = "Test6", UnitPrice = 18m, Category = category })
    .InsertEntry();
Assert.Equal("Test6", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);

Dynamic syntax

var x = ODataDynamic.Expression;
var category = client
    .For(x.Categories)
    .Set(x.CategoryName = "Test5")
    .InsertEntry();
var product = client
    .For(x.Products)
    .Set(x.ProductName = "Test6", x.UnitPrice = 18m, x.Category = category)
    .InsertEntry();
Assert.Equal("Test6", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);

Request URI: POST Products
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-08T14:43:54.5200000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test6</d:ProductName>
      <d:UnitPrice m:type="Edm.Decimal">18</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(11)" />
</entry>

See also:
Adding entries
Linking and unlinking entries
Modifying data