Silverlight アプリケーションの実装方法については、このチュートリアルに従っています。このチュートリアルでは、例として Northwind データベースを使用します。コード サンプルを使用して、チュートリアルで示したのと同じ機能を、独自のデータベースを持つ独自のアプリケーションに実装しました。チュートリアルの最後の部分では、Northwind データベースの特定のテーブルに新しいアイテムと関連するリレーションシップを追加する方法を示します。この特定の例では、次のコードを使用して、3 つのリンクされたテーブル (Product、Order、Order_Details) でこの機能を示します。
private void addDetail_Click(object sender, RoutedEventArgs e)
{
// Define a URI that returns the product with the specified ID.
Uri productUri = new Uri(svcContext.BaseUri.AbsoluteUri
+ "/Products(" + this.productId.Text + ")");
// Begin a query operation retrieve the Product object
// that is required to add a link to the new Order_Detail.
svcContext.BeginExecute<Products>(productUri,
OnProductQueryCompleted, null);
}
private void OnProductQueryCompleted(IAsyncResult result)
{
// Use the Dispatcher to ensure that the
// asynchronous call returns in the correct thread.
Dispatcher.BeginInvoke(() =>
{
// Get the Product returned by the completed query.
IEnumerable<Products> queryResult =
svcContext.EndExecute<Products>(result);
Products returnedProduct = queryResult.First();
// Get the currently selected order.
Orders currentOrder = (Orders)ordersGrid.SelectedItem;
// Create a new Order_Details object with the supplied FK values.
Order_Details newItem = Order_Details.CreateOrder_Details(currentOrder.OrderID,
returnedProduct.ProductID, 0, 0, 0);
detailsBindingCollection.Add(newItem);
// Add the new item to the context.
svcContext.AddToOrder_Details(newItem);
// Add the relationship between the order and the new item.
currentOrder.Order_Details.Add(newItem);
svcContext.AddLink(currentOrder, "Order_Details", newItem);
// Set the reference to the order and product from the item.
newItem.Orders = currentOrder;
svcContext.SetLink(newItem, "Orders", currentOrder);
// Add the relationship between the product and the new item.
returnedProduct.Order_Details.Add(newItem);
svcContext.AddLink(returnedProduct, "Order_Details", newItem);
// Set the reference to the product from the item.
newItem.Products = returnedProduct;
svcContext.SetLink(newItem, "Products", returnedProduct);
}
);
}
マイ データベース スクリプト:
CREATE TABLE InmarsatZenith.dbo.ClientJob
(JobRef nvarchar(15),
Summary text
PRIMARY KEY(JobRef))
CREATE TABLE InmarsatZenith.dbo.Job
(IntRef uniqueidentifier,
JobRef nvarchar(15),
CopyDeadline datetime,
PublicationDate datetime,
Repeat bit,
BusinessType nvarchar(25),
Sector nvarchar(30),
Lang nvarchar(15),
Format nvarchar(25),
CreativeRotation nvarchar(50),
TipinType nvarchar(25),
Status nvarchar(100)
PRIMARY KEY(IntRef))
CREATE TABLE InmarsatZenith.dbo.Detail
(IntRef uniqueidentifier,
Description nvarchar(100),
Date datetime
PRIMARY KEY(IntRef))
クライアント ジョブとジョブの間には 1 対多の関係があります (1 つのクライアント ジョブに複数のジョブを含めることができます)。そして仕事とディテールの1対1の関係。
問題は、同じ非同期メソッドを自分のアプリケーションに実装したいが、1 対 1 の関係をはるかに単純にしたいということです。したがって、基本的には、新しいジョブを「ジョブ」テーブルに追加し、関連する詳細を「ジョブの詳細」テーブルに追加できるようにしたいと考えています。このコード サンプルをこの特定のメソッドで動作するように変換しようとして、いくつか問題が発生しました。このコードを 1 対 1 の関係で動作するように適応させるための助けをいただければ幸いです。
誰かがこの主題について私を啓発することができれば、私は本当に感謝しています.
よろしくお願いします。