为什么在WCF数据服务中调用addlink和setlink

时间:2010-08-08 00:44:10

标签: wcf

考虑一个客户有很多订单。

context.AddLink(customer, "orders", order);
context.SetLink(order, "customer", customer);

数据存储将使用这些调用中的一个或另一个正确更新(在调用SaveChanges之后)。那么为什么还要在WCF数据服务的文档中反复显示这两个问题呢?

2 个答案:

答案 0 :(得分:3)

你应该同时打电话,因为你必须在两个方向手动设置链接,除非你只需要保存其中一个链接。

context.AddLink(customer, "orders", order);告诉上下文,客户与您的订单列表之间存在链接(订单必须是集合)。

context.SetLink(order, "customer", customer);告诉上下文订单已连接到客户。然后在SaveChanges上,将在数据库中创建链接,您将不会有参考问题。

您也可以调用AddRelatedObject,但它也会调用AddObject,因此该对象无法在上下文中工作。 MSDN AddRelatedObject

MSDN SetLink

复制
// Add links for the one-to-many relationships.
context.AddLink(order, "Order_Details", newItem);
context.AddLink(selectedProduct, "Order_Details", newItem);

// Set reference links for the many-to-one relationships.
context.SetLink(newItem, "Order", order);
context.SetLink(newItem, "Product", selectedProduct);

答案 1 :(得分:0)

用于在“to-many”关系中添加ref(与客户关联的新订单),调用AddLink。换句话说(“to-one”)调用setlink。

相关问题