Skip to content
object edited this page Oct 9, 2012 · 12 revisions

Simple.Data method Insert is used to create new entries in OData collections.


Insert a product by specifying all it's fields

var product = _db.Products.Insert(ProductName: "Test1", UnitPrice: 18m);
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 by specifying the entry object

var expando = new ExpandoObject();
var properties = expando as IDictionary<String, object>;
properties["ProductName"] = "Test9";
properties["UnitPrice"] = 18m;
var product = _db.Products.Insert(expando);
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:27:34.7650000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test9</d:ProductName>
      <d:UnitPrice m:type="Edm.Decimal">18</d:UnitPrice>
    </m:properties>
  </content>
</entry>

Insert a product and validate it's autogenerated ProductID

var product = _db.Products.Insert(ProductName: "Test1", 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
Simple.Data documentation for Insert