时间:2010-07-24 12:11:11

标签: silverlight wcf entity-framework silverlight-4.0

1 个答案:

答案 0 :(得分:0)

如果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);
                }
            }
        }
    }
相关问题