D365 C# - 检索多个并且更新非常慢

D365 C# - Retrieve Multiple and Updating Very Slow

我正在构建一个连接到 D365 实例的控制台,使用 fetch XML 查找记录并使用特定值更新这些记录。它有效,它正在更新记录。但是,它每秒只更新一两次,这远非理想,我有数十万条记录要更新。我见过/使用过至少每秒更新数百个的控制台应用程序。

我是不是以错误的方式去做我想做的事情?我的代码中一定有什么东西导致它非常低效/愚蠢。我还应该说,这是我第一次使用 C#,所以我可能正在做一些非常愚蠢的事情。

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;


namespace My.Crm.RetrieveMultipleConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = xxxx/;Username=xxxx;Password=xxxx";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                string fetchquery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='account'>
    <attribute name='name' />
    <order attribute='name' descending='false' />
    <filter type='and'>
      <condition attribute='name' operator='like' value='%Tom%' />
    </filter>
  </entity>
</fetch>";

                var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = false,
                        ReturnResponses = true
                    },
                    Requests = new OrganizationRequestCollection()
                };
                EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchquery));
                foreach (var c in accounts.Entities)
                {

                    Console.WriteLine("accountid: {0}", c.Attributes["name"]);                
                    Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
                    multipleRequest.Requests.Add(updateRequest);
                    c.Attributes["name"] = "New Name";
                    Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);

                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}

foreach循环中取出你的service.Execute(multipleRequest),放在外面。这样执行就真的是 ExecuteMultipleRequest.

var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
                {
                    Settings = new ExecuteMultipleSettings()
                    {
                        ContinueOnError = false,
                        ReturnResponses = true
                    },
                    Requests = new OrganizationRequestCollection()
                };
                EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchquery));
                foreach (var c in accounts.Entities)
                {

                    Console.WriteLine("accountid: {0}", c.Attributes["name"]);                
                    Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
                    multipleRequest.Requests.Add(updateRequest);
                    c.Attributes["name"] = "New Name";

                }

Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);