I need to update CRM data from external table. All working fine but its very slow. This is my code:
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();
}
}
}
With this I can insert and update data in CRM but its to slow. Is there a way to use for this Parallel.ForEach method or some other method to speed up this? Thanks!
Definitely ExecuteMultipleRequest is the way to go in that case.
The difference is that you'll send a single request so you won't have the network overhead and also, all your inserts will be processed on server-side by CRM, much quicker.