2010-05-16 64 views
4

我有三個表格,Template,Fields和TemplateFields。 TemplateFields保存每個模板的選定字段。 我需要在用戶完成選擇字段時更新TemplateFields。我能看到做到這一點的唯一方法是刪除該模板的所有TemplateField,然後在不同的請求中逐個添加它們。這非常糟糕,因爲沒有交易回落,並且還會有很多請求。使用jQuery和OData更新/插入多行(WCF數據服務)

有沒有使用WCF數據服務一次添加多個'對象'的方法?然後我可以使用攔截器來更新數據庫。

回答

1

請參閱本文 「添加/創建數據的OData/WCF數據服務批解釋」:

http://franssenden.wordpress.com/2010/06/18/addingcreating-data-to-odatawcf-data-service-batch-explained/

更新:

文章已經轉移到:
http://www.fcodings.com/2010/06/18/addingcreating-data-to-odatawcf-data-service-batch-explained/

從後報價爲每個請求的評論

  1. 服務

    using System.Collections.Generic; 
    using System.Data.Services; 
    using System.Linq; 
    using System.ServiceModel.Web; 
    using System.Web; 
    using System.Linq.Expressions; 
    using System.Data.Services.Common; 
    
    namespace TestAdventureWorksDataServices 
    { 
        public class AdventureService : DataService<AdventureWorksEntities> 
        { 
         // This method is called only once to initialize service-wide policies. 
         public static void InitializeService(DataServiceConfiguration config) 
         { 
          // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. 
          // Examples: 
          config.SetEntitySetAccessRule("*", EntitySetRights.All); 
          // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); 
          config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; 
          config.UseVerboseErrors = false; 
         } 
    
         protected override void HandleException(HandleExceptionArgs args) 
         { 
          throw new DataServiceException(args.Exception.InnerException.Message, args.Exception); 
         } 
    
        } 
    } 
    
  2. 客戶

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Data.Services.Client; 
    
    namespace Client 
    { 
        using Client.AdventureWorksServiceReference; 
    
        class Program 
        { 
         private static AdventureWorksEntities _context = null; 
    
         static void Main(string[] args) 
         { 
          _context = new AdventureWorksEntities(new Uri("http://ipv4.fiddler:51824/AdventureService.svc")); 
    
          var product1 = Product.CreateProduct(0, "My Test Product 1", "1234", true, true, 1, 1, 100, 200, 3, 
          DateTime.Now, new Guid("E29C16AE-908A-4F53-8E19-DC2CFDDF08A0"), DateTime.Now); 
    
          var product2 = Product.CreateProduct(0, "My Test Product 2", "5678", true, true, 1, 1, 200, 300, 3, 
          DateTime.Now, new Guid("1B9689D6-CCFF-40C3-AA0F-1AC3C5951738"), DateTime.Now); 
    
          var product3 = Product.CreateProduct(0, "My Test Product 3", "9876", true, true, 1, 1, 300, 400, 3, 
          DateTime.Now, new Guid("{0B677FB4-890E-4FAF-AD6A-7477D5703E6E}"), DateTime.Now); 
    
          var collection = new DataServiceCollection<Product>(_context); 
          collection.Add(product1); 
          collection.Add(product2); 
          collection.Add(product3); 
          _context.SaveChanges(); 
    
          Console.Read(); 
    
          //remove products to omit unique constraint next time running this app: 
          collection.Remove(product1); 
          collection.Remove(product2); 
          collection.Remove(product3); 
          _context.SaveChanges(SaveChangesOptions.Batch); 
    
          Console.WriteLine("Deleted. Sorry, changed my mind!"); 
          Console.Read(); 
    
         } 
        } 
    } 
    

凡下面是在客戶端代碼中最重要的部分:

_context.SaveChanges(SaveChangesOptions.Batch); 
+0

感謝您花時間回答這個問題。然而,我的問題是使用JavaScrtipt來完成......最好是jQuery(我忘了在問題中提到:s)。 – Pieter 2010-09-21 12:26:47

相關問題