2010-07-24 70 views
1

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

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

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

我只是创建了一个循环来检查实体物品的价值和值不完全更新。 BeginSaveChanges工作正常,但它只是使用未更新的值。

任何想法如何解决这个问题?

感谢

回答

0

右键完全冲洗出的SaveChanges,将显示错误消息,如果EndSaveChanges()失败,如下面的代码示例。显然你不能使用控制台在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); 
       } 
      } 
     } 
    }