none
WCF data service 中的DataServiceContext.BeginSaveChanges()方法是怎么用的? RRS feed

  • 问题

  • 我想在应用商店应用中使用WCF data service往SQLServer数据库中添加数据(以Nothwind数据库为例).

    下面是从silverlight借鉴过来部分代码:

    NorthwindEntities context;

    private void saveChangesButton_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    Orders Orders1 = Orders.CreateOrders(2000);
                    Orders1.CustomerID = "ALFKI";
                    Orders1.ShipCity = "Beijing";
                    context.AddToOrders(Orders1);
                    // Start the saving changes operation. This needs to be a
                    // batch operation in case we are added a new object with
                    // a new relationship.
                    context.BeginSaveChanges(SaveChangesOptions.Batch,
                        OnChangesSaved, context);
                }
                catch (Exception ex)
                {
                    tb1.Text = string.Format("The changes could not be saved to the data service.\n"
                         + "The following error occurred: {0}", ex.Message);
                }
            }

            private void OnChangesSaved(IAsyncResult result)
            {
                bool errorOccured = false;

                // Use the Dispatcher to ensure that the
                // asynchronous call returns in the correct thread.
                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    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)
                        {
                            tb1.Text = "The changes have been saved to the data service.";
                        }
                        else
                        {
                            tb1.Text = ("An error occured. One or more changes could not be saved.");
                        }
                    }
                    catch (Exception ex)
                    {
                        // Display the error from the response.
                        tb1.Text = (string.Format("The following error occured: {0}", ex.Message));
                    }
                }
                ).AsTask().Wait();
            }

    但是这段代码无法通过调试,我想知道出了什么问题。


    王林

    2013年9月11日 14:14

全部回复

  • Hi wanglin005,

    请参考这两篇文档:Calling Service Operations 和 Writing a Windows Store App that consumes an OData Service,另外你的代码无法通过调试,具体的信息是什么?

    Best Regards,

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月12日 9:41
    版主
  • hi, James

    您提供的文档非常有用,让我慢慢消化一下。

    上面的代码运行到 DataServiceResponse response = context.EndSaveChanges(result);这一句时错误信息是“处理此请求时出错”,请帮忙给分析一下。 多谢!


    王林

    2013年9月12日 13:46
  • Hi wanglin005,

    回复晚了抱歉。关于你的错误,我建议你看一下这篇文章 http://msdn.microsoft.com/zh-cn/magazine/hh580732.aspx 通过自定义错误消息来分析具体的错误信息,如果光光是“处理此请求时出错”很难让人判断问题出在何处。

    我个人推测很有可能是EF的并发问题,CDSN上有个帖子我觉得你可以参考下:http://bbs.csdn.net/topics/360176653,在使用之前更新缓存以保证不出错。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月16日 8:58
    版主
  • hi, James

    您提供的第一篇文章 http://msdn.microsoft.com/zh-cn/magazine/hh580732.aspx中提到的System.Data.Entity.Validation 命名空间在应用商店应用中找不到,所以不能使用其中的代码来获取错误信息。

    如果像您所说是并发问题,那么代码应该怎样修改才能通过。

    多谢!


    王林

    2013年9月17日 13:14