jquery - 使用 jQuery 和 OData(WCF 数据服务)更新/插入多行

标签 jquery wcf service odata

我有三个表:Template、Fields 和 TemplateFields。 TemplateFields 保存每个模板的选定字段。 当用户完成选择字段时,我需要更新 TemplateFields。我能想到的唯一方法是删除该模板的所有 TemplateField,然后在单独的请求中将它们一一添加。这真的很糟糕,因为没有事务可以回退,而且还会有很多请求。

有没有办法使用 WCF 数据服务一次添加多个“对象”?然后我可以使用拦截器来更新数据库。

最佳答案

请参阅这篇文章“向 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);

关于jquery - 使用 jQuery 和 OData(WCF 数据服务)更新/插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844096/

相关文章:

jquery - 如何使用CSS正确地并排 float 两列

c# - 带有 xs :anytype causing serialization problems 的 WCF 消息

asp.net - WCF服务支持文件jsdebug加载失败

android - 将 GPS 坐标发送到服务器的服务

java - 从服务访问 View

jquery - fnGetData() undefined 不是函数错误

javascript - 像 github 一样的表情符号自动完成?

javascript - 为什么 jQuery ajax 响应总是被填满

c# - 检查 HttpWebResponse 是否为空

service - Windows 10 应用服务