【问题标题】:wcf data services saving relational data in single clickwcf 数据服务一键保存关系数据
【发布时间】:2012-06-16 11:50:25
【问题描述】:

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

想想 2 表:类别和产品

我要保存数据界面:

来自类别表:-
CategoryId :(自动递增)
类别名称:abc

来自产品表:-
ProductId :-(自动递增)
类别 ID:- ? (不知道如何检索)
产品名称: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));
}

【问题讨论】:

    标签: wcf silverlight windows-phone-7 wcf-data-services wcf-ria-services


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多