0

我在 Windows phone 7 中使用 WCF 数据服务,我想通过单击保存关系数据,我该怎么做?

它的想法 2 表:类别和产品

我想保存数据 UI:

从类别表:-
CategoryId :(自动递增)
CategoryName :abc

从产品表:-
ProductId :-(自动增量)
CategoryId :- ?(不知道如何检索)
ProductsName : xyz

在按钮保存点击:

我想在适当的表中插入上述数据,我该怎么做?

我正在使用以下代码添加一个表数据:

try
{
     context = new NorthwindEntities(NorthwindUri);
     context.AddToProducts(product);
     context.BeginSaveChanges(new AsyncCallback((result) =>
     {
         bool errorOccured = false;

         // Use the Dispatcher to ensure that the 
         // asynchronous call returns in the correct thread.
         Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
             context = result.AsyncState as NorthwindEntities;

             try
             {
                  // Complete the save changes operation and display the response.
                  DataServiceResponse response = context.EndSaveChanges(result);

                  foreach (ChangeOperationResponse changeResponse in response)
                  {
                      if (changeResponse.Error != null) errorOccured = true;
                  }
                  if (!errorOccured)
                  {
                      MessageBox.Show("The changes have been saved to the data service.");
                  }
                  else
                  {
                      MessageBox.Show("An error occured. One or more changes could not be saved.");
                  }
              }
              catch (Exception ex)
              {
                   // Display the error from the response.
                   MessageBox.Show(string.Format("The following error occured: {0}", ex.Message));
              }
         });
    }), context);
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("The changes could not be saved to the data service.\n"
        + "The following error occurred: {0}", ex.Message));
}
4

1 回答 1

1

在 OData 中,关系不表示为外键,而是表示为导航属性。然后您通过操作客户端库中的链接来操作它们。看看这篇文章: http: //msdn.microsoft.com/en-us/library/dd756361 (v=vs.103).aspx 您可以调用多个修改数据的方法,然后调用 SaveChanges 将它们全部发送到服务器。但请注意,如果服务器需要参照完整性,并且您例如同时添加两个相关实体,您可能需要使用 SaveChanges(Batch) (这使得客户端在一个请求中发送所有内容,从而允许服务器将其作为单个事务处理)。

于 2012-06-16T18:19:12.720 回答