Skip to content
Vagif Abilov edited this page Nov 20, 2013 · 26 revisions

Insert a product

Untyped syntax

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

Typed syntax

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

Dynamic syntax

var x = ODataDynamic.Expression;
var product = client
    .For(x.Products)
    .Set(
        x.ProductName = "Test1", 
        x.UnitPrice = 18m
    )
    .InsertEntry();
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 it's autogenerated ProductID

Untyped syntax

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

Typed syntax

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

Dynamic syntax

var x = ODataDynamic.Expression;
var product = client
    .For(x.Products)
    .Set(
        x.ProductName = "Test1", 
        x.UnitPrice = 18m
    );
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