private void SaveChangesHelper(params Entity[] entities) { SaveChangesResultCollection results = _orgContext.SaveChanges(); if (results.HasError) { DeleteRequiredRecords(false); StringBuilder sb = new StringBuilder(); foreach (var result in results) { if (result.Error != null) { sb.AppendFormat("Error: {0}\n", result.Error.Message); } } throw new InvalidOperationException("Failure occurred while " + "calling OrganizationServiceContext.SaveChanges()\n" + sb.ToString()); } else { foreach (Entity e in entities) { e.EntityState = null; } } }
/// <summary> /// Saves the changes for the given CRM organization context. /// </summary> /// <param name="saveChangesOptions">SaveChangesOptions</param> /// <returns>A collection of the action results <see cref="SaveChangesResultCollection"/> class.</returns> public virtual SaveChangesResultCollection SaveChanges(SaveChangesOptions saveChangesOptions) { Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name)); SaveChangesResultCollection scrc = OrganizationServiceContext.SaveChanges(saveChangesOptions); Trace(string.Format(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name)); return(scrc); }
protected override void OnSaveChanges(SaveChangesResultCollection results) { // reattach entities that are found in the save changes results if (!results.HasError) { var entities = results.SelectMany(result => GetEntitiesForObject(result.Request)); foreach (var entity in entities) { // reset the state entity.EntityState = null; Attach(entity); } } }
public static bool BulkSave(EntityCollection entities, CrmConnection connection = null) { OrganizationService srv = new OrganizationService(connection ?? XrmConnection.Connection); using (CrmOrganizationServiceContext service = new CrmOrganizationServiceContext(srv)) { foreach (Entity e in entities.Entities) { if (e.Id.Equals(Guid.Empty)) { e.Id = Guid.NewGuid(); // service.Attach(e); service.AddObject(e); } else { service.Attach(e); service.UpdateObject(e); } } SaveChangesResultCollection result = service.SaveChanges(Microsoft.Xrm.Sdk.Client.SaveChangesOptions.None); return(!result.HasError); } }
protected override void OnSaveChanges(SaveChangesResultCollection results) { _deltaEntities.Clear(); base.OnSaveChanges(results); }