private async Task <string> TrasnferAudit() { StringBuilder content = new StringBuilder(); await _comparerSource.InitializeConnection(_iWriteToOutput, content, "Connection CRM Source.", "Connection CRM Target."); string operation = string.Format(Properties.OperationNames.TransferingAuditFormat2, ConnectionSource.Name, ConnectionTarget.Name); content.AppendLine(_iWriteToOutput.WriteToOutputStartOperation(null, operation)); var repositorySource = new EntityMetadataRepository(_comparerSource.Service1); var repositoryTarget = new EntityMetadataRepository(_comparerSource.Service2); var taskSource = repositorySource.GetEntitiesWithAttributesForAuditAsync(); var taskTarget = repositoryTarget.GetEntitiesWithAttributesForAuditAsync(); var listEntityMetadataSource = await taskSource; content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Entities in {0}: {1}", ConnectionSource.Name, listEntityMetadataSource.Count())); var listEntityMetadataTarget = await taskTarget; content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Entities in {0}: {1}", ConnectionTarget.Name, listEntityMetadataTarget.Count())); var commonEntityMetadata = new List <LinkedEntities <EntityMetadata> >(); foreach (var entityMetadata1 in listEntityMetadataSource.OrderBy(e => e.LogicalName)) { { var entityMetadata2 = listEntityMetadataTarget.FirstOrDefault(e => string.Equals(e.LogicalName, entityMetadata1.LogicalName, StringComparison.InvariantCultureIgnoreCase)); if (entityMetadata2 != null) { commonEntityMetadata.Add(new LinkedEntities <EntityMetadata>(entityMetadata1, entityMetadata2)); continue; } } } HashSet <string> entitiesToPublish = new HashSet <string>(); var entitiesToEnableAudit = commonEntityMetadata.Where( e => e.Entity1.IsAuditEnabled != null && e.Entity1.IsAuditEnabled.Value && e.Entity2.IsAuditEnabled != null && e.Entity2.IsAuditEnabled.CanBeChanged && e.Entity2.IsAuditEnabled.Value == false ).ToList(); if (entitiesToEnableAudit.Any()) { content .AppendLine() .AppendFormat("Enabling Audit on Entities: {0}", entitiesToEnableAudit.Count) .AppendLine(); foreach (var entityLink in entitiesToEnableAudit.OrderBy(e => e.Entity1.LogicalName)) { content.AppendLine(_tabSpacer + entityLink.Entity1.LogicalName); entitiesToPublish.Add(entityLink.Entity1.LogicalName); try { entityLink.Entity2.IsAuditEnabled.Value = true; await repositoryTarget.UpdateEntityMetadataAsync(entityLink.Entity2); } catch (Exception ex) { var desc = DTEHelper.GetExceptionDescription(ex); content.AppendLine(desc); } } } bool first = true; foreach (var entityLink in commonEntityMetadata.OrderBy(e => e.Entity1.LogicalName)) { var query = from source in entityLink.Entity1.Attributes join target in entityLink.Entity2.Attributes on source.LogicalName equals target.LogicalName where source.IsAuditEnabled != null && string.IsNullOrEmpty(source.AttributeOf) && string.IsNullOrEmpty(target.AttributeOf) && source.IsAuditEnabled.Value && target.IsAuditEnabled != null && target.IsAuditEnabled.CanBeChanged && target.IsAuditEnabled.Value == false orderby target.LogicalName select target; foreach (var attribute in query) { if (first) { content .AppendLine() .AppendLine("Enabling Audit on Attributes:") .AppendLine(); first = false; } content .AppendFormat(_tabSpacer + "{0}.{1}", attribute.EntityLogicalName, attribute.LogicalName) .AppendLine(); entitiesToPublish.Add(attribute.EntityLogicalName); try { attribute.IsAuditEnabled.Value = true; await repositoryTarget.UpdateAttributeMetadataAsync(attribute); } catch (Exception ex) { var desc = DTEHelper.GetExceptionDescription(ex); content.AppendLine(desc); } } } if (entitiesToPublish.Any()) { content .AppendLine() .AppendFormat("Publish Entities: {0}", entitiesToPublish.Count) .AppendLine(); foreach (var item in entitiesToPublish.OrderBy(s => s)) { content.AppendLine(_tabSpacer + item); } var repositoryPublish = new PublishActionsRepository(_comparerSource.Service2); try { await repositoryPublish.PublishEntitiesAsync(entitiesToPublish); } catch (Exception ex) { var desc = DTEHelper.GetExceptionDescription(ex); content.AppendLine(desc); } } content.AppendLine().AppendLine().AppendLine(_iWriteToOutput.WriteToOutputEndOperation(null, operation)); string fileName = string.Format("OrgTransfer Audit from {0} to {1} at {2}.txt" , this.ConnectionSource.Name , this.ConnectionTarget.Name , DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss")); string filePath = Path.Combine(_folder, FileOperations.RemoveWrongSymbols(fileName)); File.WriteAllText(filePath, content.ToString(), new UTF8Encoding(false)); return(filePath); }