方法: エンティティ リレーションシップを定義する (WCF Data Services)
WCF Data Services で新しいエンティティを追加するとき、新しいエンティティと関連エンティティの間のリレーションシップは自動的に定義されません。 エンティティ インスタンス間のリレーションシップを作成および変更し、クライアント ライブラリを使用して、これらの変更をデータ サービスに反映できます。 詳細については、「データ サービスの更新 (WCF Data Services)」を参照してください。
このトピックの例では、Northwind サンプル データ サービスおよび自動生成されたクライアント データ サービス クラスを使用します。 このサービスおよびクライアント データ クラスは、WCF Data Services クイック スタートを完了したときに作成されます。
例
次の例では、新しいオブジェクト インスタンスを作成し、DataServiceContext で AddRelatedObject メソッドを呼び出して、関連する注文へのリンクと共に、コンテキスト内に項目を作成します。 SaveChanges メソッドが呼び出されたときに HTTP POST メッセージがデータ サービスに送信されます。
Dim productId = 25
Dim customerId = "ALFKI"
Dim newItem As Order_Detail = Nothing
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Try
' Get the specific product.
Dim selectedProduct = (From product In context.Products _
Where product.ProductID = productId _
Select product).Single()
' Get the specific customer.
Dim cust = (From customer In context.Customers.Expand("Orders") _
Where customer.CustomerID = customerId _
Select customer).Single()
' Get the first order.
Dim order = cust.Orders.FirstOrDefault()
' Create a new order detail for the specific product.
newItem = Order_Detail.CreateOrder_Detail( _
order.OrderID, selectedProduct.ProductID, 10, 5, 0)
' Add the new item with a link to the related order.
context.AddRelatedObject(order, "Order_Details", newItem)
' Since the item is now tracked by the context,
' set just the link to the related product.
context.AddLink(selectedProduct, "Order_Details", newItem)
' Add the new order detail to the collection, and
' set the reference to the product.
order.Order_Details.Add(newItem)
newItem.Order = order
newItem.Product = selectedProduct
' Send the inserts to the data service.
context.SaveChanges()
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred when saving changes.", ex)
' Handle any errors that may occur during insert, such as
' a constraint violation.
Catch ex As DataServiceRequestException
Throw New ApplicationException( _
"An error occurred when saving changes.", ex)
int productId = 25;
string customerId = "ALFKI";
Order_Detail newItem = null;
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
try
{
// Get the specific product.
var selectedProduct = (from product in context.Products
where product.ProductID == productId
select product).Single();
// Get the specific customer.
var cust = (from customer in context.Customers.Expand("Orders")
where customer.CustomerID == customerId
select customer).Single();
// Get the first order.
Order order = cust.Orders.FirstOrDefault();
// Create a new order detail for the specific product.
newItem = Order_Detail.CreateOrder_Detail(
order.OrderID, selectedProduct.ProductID, 10, 5, 0);
// Add the new item with a link to the related order.
context.AddRelatedObject(order, "Order_Details", newItem);
// Since the item is now tracked by the context,
// set just the link to the related product.
context.AddLink(selectedProduct, "Order_Details", newItem);
// Add the new order detail to the collection, and
// set the reference to the product.
order.Order_Details.Add(newItem);
newItem.Order = order;
newItem.Product = selectedProduct;
// Send the inserts to the data service.
context.SaveChanges();
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred when saving changes.", ex);
}
// Handle any errors that may occur during insert, such as
// a constraint violation.
catch (DataServiceRequestException ex)
{
throw new ApplicationException(
"An error occurred when saving changes.", ex);
}
次の例では、AddObject メソッドを使用して、特定の Products
オブジェクトへの参照と共に、Order_Details
オブジェクトを関連する Orders
オブジェクトに追加する方法を示します。 AddLink メソッドおよび SetLink メソッドは、リレーションシップを定義します。 次の例では、Order_Details
オブジェクトのナビゲーション プロパティも明示的に設定されます。
Dim productId = 25
Dim customerId = "ALFKI"
Dim newItem As Order_Detail = Nothing
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Try
' Get the specific product.
Dim selectedProduct = (From product In context.Products _
Where product.ProductID = productId _
Select product).Single()
' Get the specific customer.
Dim cust = (From customer In context.Customers.Expand("Orders") _
Where customer.CustomerID = customerId _
Select customer).Single()
' Get the first order.
Dim order = cust.Orders.FirstOrDefault()
' Create a new order detail for the specific product.
newItem = Order_Detail.CreateOrder_Detail( _
order.OrderID, selectedProduct.ProductID, 10, 5, 0)
' Add the new order detail to the context.
context.AddToOrder_Details(newItem)
' Add links for the one-to-many relationships.
context.AddLink(order, "Order_Details", newItem)
context.AddLink(selectedProduct, "Order_Details", newItem)
' Add the new order detail to the collection, and
' set the reference to the product.
order.Order_Details.Add(newItem)
newItem.Product = selectedProduct
' Send the inserts to the data service.
context.SaveChanges()
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred when saving changes.", ex)
' Handle any errors that may occur during insert, such as
' a constraint violation.
Catch ex As DataServiceRequestException
Throw New ApplicationException( _
"An error occurred when saving changes.", ex)
int productId = 25;
string customerId = "ALFKI";
Order_Detail newItem = null;
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
try
{
// Get the specific product.
var selectedProduct = (from product in context.Products
where product.ProductID == productId
select product).Single();
// Get the specific customer.
var cust = (from customer in context.Customers.Expand("Orders")
where customer.CustomerID == customerId
select customer).Single();
// Get the first order.
Order order = cust.Orders.FirstOrDefault();
// Create a new order detail for the specific product.
newItem = Order_Detail.CreateOrder_Detail(
order.OrderID, selectedProduct.ProductID, 10, 5, 0);
// Add the new order detail to the context.
context.AddToOrder_Details(newItem);
// Add links for the one-to-many relationships.
context.AddLink(order, "Order_Details", newItem);
context.AddLink(selectedProduct, "Order_Details", newItem);
// Add the new order detail to the collection, and
// set the reference to the product.
order.Order_Details.Add(newItem);
newItem.Product = selectedProduct;
// Send the inserts to the data service.
context.SaveChanges();
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred when saving changes.", ex);
}
// Handle any errors that may occur during insert, such as
// a constraint violation.
catch (DataServiceRequestException ex)
{
throw new ApplicationException(
"An error occurred when saving changes.", ex);
}
参照
処理手順
方法: エンティティを追加、変更、および削除する (WCF Data Services)