【问题标题】:WCF Data Services UpdateObject not workingWCF 数据服务更新对象不起作用
【发布时间】:2010-07-24 12:11:11
【问题描述】:

我有一个 Silverlight 客户端,它带有一个从 WCF 数据服务获取数据的网格。工作正常。

但是,如果我想更新一些更改的网格行,则服务数据上下文 UpdateObject 不起作用:

    DataServiceContext.UpdateObject(MyGrid.SelectedItem);
    foreach (Object item in DataServiceContext.Entities)
    {
        //
    }
    DataServiceContext.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, DataServiceContext);

我刚刚创建了一个循环来检查实体项的值,并且该值根本没有更新。 BeginSaveChanges 工作正常,但它只是使用未更新的值。

有什么办法解决这个问题吗?

谢谢

【问题讨论】:

    标签: silverlight wcf entity-framework silverlight-4.0


    【解决方案1】:

    如果 EndSaveChanges() 失败,将显示错误消息的完全刷新的 SaveChanges,如下面的代码示例。显然你不能使用控制台在 silverlight 中写出你的消息,但你明白了。

    例如,当我编写以下示例时,我发现我收到了一个禁止错误,因为我的实体集有 EntitySetRights.AllRead,而不是 EntitySetRights.All

     class Program
        {
            private static AdventureWorksEntities svc;
    
            static void Main(string[] args)
            {
                svc =
                    new AdventureWorksEntities(
                        new Uri("http://localhost:5068/AWDataService.svc", 
                            UriKind.Absolute));
                var productQuery = from p in svc.Products
                        where p.ProductID == 740
                        select p;
                var product = productQuery.First();
                ShowProduct(product);
                product.Color = product.Color == "Silver" ? "Gray" : "Silver";
                svc.UpdateObject(product);
                svc.BeginSaveChanges(SaveChangesOptions.Batch, OnSave, svc);
                ShowProduct(product);
                Console.ReadKey();
            }
    
            private static void ShowProduct(Product product)
            {
                Console.WriteLine("Id: {0} Name: {1} Color: {2}", 
                    product.ProductID, product.Name, product.Color);
            }
    
            private static void OnSave(IAsyncResult ar)
            {
                svc = ar.AsyncState as AdventureWorksEntities;
                try
                {
                    WriteResponse(svc.EndSaveChanges(ar));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    
            private static void WriteResponse(DataServiceResponse response)
            {
                if(response.IsBatchResponse)
                {
                    Console.WriteLine("Batch Response Code: {0}", response.BatchStatusCode);
                }
                foreach (ChangeOperationResponse change in response)
                {
                    Console.WriteLine("Change code: {0}", change.StatusCode);
                    if(change.Error != null)
                    {
                        Console.WriteLine("\tError: {0}", change.Error.Message);
                    }
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2020-10-27
      • 2011-12-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2011-03-06
      相关资源
      最近更新 更多