private static string BuildFetchXml(CrmRelationship relationship, CrmEntity entity, List <IMappingFetchCreator> mappingFetchCreators)
        {
            StringBuilder fetchXML = new StringBuilder();

            fetchXML.AppendLine("<fetch version=\"1.0\" output-format=\"xml - platform\" mapping=\"logical\" distinct=\"false\">");
            fetchXML.AppendLine("<entity name=\"" + relationship.RelatedEntityName + "\">");

            fetchXML.AppendLine("<attribute name=\"" + entity.PrimaryIdField + "\" />");
            fetchXML.AppendLine("<attribute name=\"" + relationship.TargetEntityPrimaryKey + "\" />");

            var aplicableFetchCreators = mappingFetchCreators.Where(mr => mr.UseForEntity(relationship.RelatedEntityName)).ToList();

            foreach (var rule in aplicableFetchCreators)
            {
                fetchXML.Append(rule.GetExportFetchXML(relationship.RelatedEntityName, new CrmField()
                {
                    LookupType = entity.Name, FieldName = entity.PrimaryIdField
                }));
            }

            foreach (var rule in aplicableFetchCreators)
            {
                fetchXML.Append(rule.GetExportFetchXML(relationship.RelatedEntityName, new CrmField()
                {
                    LookupType = relationship.TargetEntityName, FieldName = relationship.TargetEntityPrimaryKey
                }));
            }

            fetchXML.AppendLine("</entity>");
            fetchXML.AppendLine("</fetch>");

            return(fetchXML.ToString());
        }
        public void StoreCrmEntityRelationShipData(CrmEntity crmEntity, ManyToManyRelationshipMetadata relationship, List <CrmRelationship> relationshipList)
        {
            var crmRelationShip = new CrmRelationship
            {
                RelatedEntityName      = relationship.IntersectEntityName,
                ManyToMany             = true,
                IsReflexive            = relationship.IsCustomizable.Value,
                TargetEntityPrimaryKey = crmEntity.PrimaryIdField == relationship.Entity2IntersectAttribute ? relationship.Entity1IntersectAttribute : relationship.Entity2IntersectAttribute,
                TargetEntityName       = crmEntity.Name == relationship.Entity2LogicalName ? relationship.Entity1LogicalName : relationship.Entity2LogicalName,
                RelationshipName       = relationship.IntersectEntityName
            };

            relationshipList.Add(crmRelationShip);
        }
        private List <string> GetHeader(CrmExportedDataStore store)
        {
            store.ThrowArgumentNullExceptionIfNull(nameof(store));

            var           ent    = store.ExportedEntities.FirstOrDefault();
            List <string> header = new List <string>();

            if (!ent.IsManyToMany)
            {
                CrmEntity entity = schemaConfig.Entities.FirstOrDefault(p => p.Name == ent.LogicalName);

                var fields = entity.CrmFields.Where(p => p.FieldName != entity.PrimaryIdField).Select(p => p.FieldName).ToList();

                AddFieldWithCheck(header, entity.PrimaryIdField);

                if (fields != null)
                {
                    foreach (var field in fields)
                    {
                        AddFieldWithCheck(header, field);
                    }
                }
            }
            else
            {
                CrmEntity entity = schemaConfig.Entities
                                   .First(r => r.CrmRelationships.Select(a => a.RelatedEntityName == ent.LogicalName).Any());

                CrmRelationship rel = entity.CrmRelationships.FirstOrDefault(a => a.RelatedEntityName == ent.LogicalName);

                AddFieldWithCheck(header, $"{rel.RelationshipName}id");
                AddFieldWithCheck(header, entity.PrimaryIdField);
                AddFieldWithCheck(header, rel.TargetEntityPrimaryKey);
            }

            foreach (var item in store.ExportedEntities)
            {
                var mapAtr = item.Attributes.Where(p => p.AttributeType == "Microsoft.Xrm.Sdk.AliasedValue" && !header.Contains(p.AttributeName)).Select(p => p.AttributeName).ToList();

                foreach (var field in mapAtr)
                {
                    AddFieldWithCheck(header, field);
                }
            }

            return(header);
        }