【问题标题】:Using Parallel.ForEach for insert and update CRM data使用 Parallel.ForEach 插入和更新 CRM 数据
【发布时间】:2016-10-05 03:11:03
【问题描述】:

我需要从外部表更新 CRM 数据。一切正常,但速度很慢。这是我的代码:

static void Main(string[] args)
{
var dbClient = new CollectionEntities(); //Get database Entities
using(var xrm = new XrmServiceContext("Xrm"); // Get CRM Entities
    {
foreach (var row in dbClient.Client) //Reading rows from database
{
var c = (from a in crm.new_clientSet where a.new_Idnumber == row.Client_ID select a).FirstOrDefault(); // IS there clint with id from database
                            if (c == null)// if client not exist i create new if exists I update data
                            {
                                c = new new_client { new_Idnumber = row.Client_ID };
                                crm.AddObject(c);
                            }
                            c.new_name = row.Client_name; //[Client_name]
                            c.new_Idnumber = row.Client_ID;//[Client_ID]
                            c.EmailAddress = row.Email;//[Email]
                xrm.AddObject(c);
                    xrm.SaveChanges();

}
}
}

有了这个,我可以在 CRM 中插入和更新数据,但速度很慢。有没有办法使用这个 Parallel.ForEach 方法或其他方法来加速这个?谢谢!

【问题讨论】:

    标签: c# for-loop parallel-processing dynamics-crm-2011 dynamics-crm


    【解决方案1】:

    ExecuteMultipleRequest 在这种情况下肯定是要走的路。

    不同之处在于您将发送一个请求,因此您不会有网络开销,而且您的所有插入都将由 CRM 在服务器端处理,速度更快。

    【讨论】:

      【解决方案2】:

      您在每个循环中从 CRM 加载单行。这使您的应用程序非常“健谈”,并且它在网络开销上花费的时间比加载数据要多。尝试在循环之前使用单个查询将整个 CRM 数据集加载到内存中。然后,在循环中,从内存中查找记录而不是查询 CRM。如果您有一个大型数据集,您可能需要使用分页 cookie。

      【讨论】:

        【解决方案3】:

        查看来自 Microsoft Premier Field Engineering - Dynamics 团队的开源 PFE Core Library for Dynamics CRM 库。它为您处理并行性。 Parallel Common Request 示例页面显示了并行更新一堆记录是多么容易:

        public void ParallelUpdate(List<Entity> targets)
        {
            try
            {
                this.Manager.ParallelProxy.Update(targets);
            }
            catch (AggregateException ae)
            {
                // Handle exceptions
            }
        }
        

        您还可以使用它来查询大型数据集...它将为您处理检索所有内容。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-10
          • 1970-01-01
          相关资源
          最近更新 更多