Skip to content
glenfisk edited this page Apr 7, 2022 · 26 revisions

Insert a product

Untyped syntax

var product = await client
    .For("Products")
    .Set(new { ProductName = "Test1", UnitPrice = 18m })
    .InsertEntryAsync();
Assert.Equal("Test1", product["ProductName"]);

Typed syntax

var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test1", UnitPrice = 18m })
    .InsertEntryAsync();
Assert.Equal("Test1", product.ProductName);

Dynamic syntax

var x = ODataDynamic.Expression;
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test1", x.UnitPrice = 18m)
    .InsertEntryAsync();
Assert.Equal("Test1", product.ProductName);

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:02:51.8990000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test1</d:ProductName>
      <d:UnitPrice m:type="Edm.Decimal">18</d:UnitPrice>
    </m:properties>
  </content>
</entry>

Insert a product and validate its auto generated ProductID

Untyped syntax

var product = await client
    .For("Products")
    .Set(new { ProductName = "Test1", UnitPrice = 18m })
    .InsertEntryAsync();
Assert.True((int)product["ProductID"] > 0);

Typed syntax

var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test1", UnitPrice = 18m })
    .InsertEntryAsync();
Assert.True((int)product["ProductID"] > 0);

Dynamic syntax

var x = ODataDynamic.Expression;
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test1", x.UnitPrice = 18m)
    .InsertEntryAsync();
Assert.True(product.ProductID > 0);

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:02:51.8990000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test1</d:ProductName>
      <d:UnitPrice m:type="Edm.Decimal">18</d:UnitPrice>
    </m:properties>
  </content>
</entry>

See also:
Adding entries with links
Modifying data