Linking and unlinking entries

object edited this page Oct 15, 2012 · 14 revisions

Simple.Data method Update is used to manage links between entries in OData collections.


Link a product to a category

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

Request URI: PUT Products(79)/$links/Category
Request content:

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">Categories(10)</uri>

Link a product to a different category

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

Request URI: PUT Products(80)/$links/Category
Request content:

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">Categories(11)</uri>

Unlink a product from a category

var category = _db.Categories.Insert(CategoryName: "Test6");
var product = _db.Products.Insert(ProductName: "Test7", UnitPrice: 18m, Category: category);
product = _db.Products.FindByProductName("Test7");
_db.Products.UpdateByProductName(ProductName: "Test7", Category: category);
product = _db.Products.FindByProductName("Test7");
Assert.Equal(category.CategoryID, product.CategoryID);
_db.Products.UpdateByProductName(ProductName: "Test7", Category: null);
product = _db.Products.FindByProductName("Test7");
Assert.Null(product.CategoryID);

Request URI: DELETE Products(81)/$links/Category


Link a category to multiple products

var category = _db.Categories.Insert(CategoryName: "Test3");
var product1 = _db.Products.Insert(ProductName: "Test4", UnitPrice: 21m, CategoryID: 1);
var product2 = _db.Products.Insert(ProductName: "Test5", UnitPrice: 22m, CategoryID: 1);
_db.Categories.UpdateByCategoryName(CategoryName: "Test3", Products: new[] { product1, product2 });
category = _db.Category.WithProducts().FindByCategoryName("Test3");
Assert.Equal(2, category.Products.Count);

Request URI: MERGE Categories(13)/$links/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-15T14:45:21.7200000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties />
  </content>
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=Entry" title="Products" href="Products(110)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=Entry" title="Products" href="Products(111)" />
</entry>

See also:
Adding entries with links
Updating entries with links
Modifying data