Skip to content

Adding entries with links

object edited this page Oct 11, 2012 · 8 revisions

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


Insert a product with associated category specified by CategoryID

var category = _db.Categories.Insert(CategoryName: "Test3");
var product = _db.Products.Insert(ProductName: "Test4", UnitPrice: 18m, CategoryID: category.CategoryID);
Assert.Equal("Test4", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);
category = _db.Category.WithProducts().FindByCategoryName("Test3");
Assert.True(category.Products.Count == 1);

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

var category = _db.Categories.Insert(CategoryName: "Test5");
var product = _db.Products.Insert(ProductName: "Test6", UnitPrice: 18m, Category: category);
Assert.Equal("Test6", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);
category = _db.Category.WithProducts().FindByCategoryName("Test5");
Assert.True(category.Products.Count == 1);

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>

Insert a product specified by object that contains the associated category

var category = _db.Categories.Insert(CategoryName: "Test7");
var productRecord = new { ProductName = "Test8", UnitPrice = 18m, Category = category};
var product = _db.Products.Insert(productRecord);
Assert.Equal("Test8", product.ProductName);
Assert.Equal(category.CategoryID, product.CategoryID);
category = _db.Category.WithProducts().FindByCategoryName("Test7");
Assert.True(category.Products.Count == 1);

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:46:37.9520000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:ProductName>Test8</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(12)" />
</entry>

See also:
Adding entries
Linking and unlinking entries
Modifying data
Simple.Data documentation for Insert