public static void UpdateCache(List <OneToManyRelationshipMetadata> relMetadataList, MappingEntity mappingEntity
                                       , MappingField[] properties)
        {
            // update modified fields
            var modifiedRelations = mappingEntity.RelationshipsManyToOne
                                    .Where(relation => relMetadataList.Exists(relMeta => relMeta.MetadataId == relation.MetadataId)).ToList();

            modifiedRelations.ForEach(
                rel => Update(relMetadataList.First(relMeta => relMeta.MetadataId == rel.MetadataId), rel, properties));

            // add new attributes
            var newRelMeta = relMetadataList
                             .Where(
                relMeta =>
                !Array.Exists(mappingEntity.RelationshipsManyToOne, relation => relation.MetadataId == relMeta.MetadataId))
                             .ToList();

            newRelMeta.ForEach(
                relMeta =>
            {
                var newRelations = new MappingRelationshipN1[mappingEntity.RelationshipsManyToOne.Length + 1];
                Array.Copy(mappingEntity.RelationshipsManyToOne, newRelations, mappingEntity.RelationshipsManyToOne.Length);
                mappingEntity.RelationshipsManyToOne = newRelations;
                mappingEntity.RelationshipsManyToOne[mappingEntity.RelationshipsManyToOne.Length - 1] = Parse(relMeta, properties);
            });
        }
        public static void Update(OneToManyRelationshipMetadata rel, MappingRelationshipN1 relationshipOneToMany,
                                  MappingField[] properties)
        {
            var propertyName = relationshipOneToMany.ForeignKey;

            if (rel.ReferencingAttribute != null)
            {
                var property = properties.First(p => p.Attribute.LogicalName.ToLower() == rel.ReferencingAttribute.ToLower());
                propertyName = property.DisplayName;
                relationshipOneToMany.FromField = property;
            }

            relationshipOneToMany.Attribute.ToEntity           = rel.ReferencedEntity ?? relationshipOneToMany.Attribute.FromEntity;
            relationshipOneToMany.Attribute.ToKey              = rel.ReferencedAttribute ?? relationshipOneToMany.Attribute.FromKey;
            relationshipOneToMany.Attribute.FromEntity         = rel.ReferencingEntity ?? relationshipOneToMany.Attribute.ToEntity;
            relationshipOneToMany.Attribute.FromKey            = rel.ReferencingAttribute ?? relationshipOneToMany.Attribute.ToKey;
            relationshipOneToMany.Attribute.IntersectingEntity = "";
            relationshipOneToMany.ForeignKey = propertyName;

            if (rel.SchemaName != null)
            {
                relationshipOneToMany.DisplayName = Naming.GetProperVariableName(rel.SchemaName);
                relationshipOneToMany.SchemaName  = Naming.GetProperVariableName(rel.SchemaName);
                relationshipOneToMany.PrivateName = "_n1" + Naming.GetEntityPropertyPrivateName(rel.SchemaName);
                relationshipOneToMany.HybridName  = Naming.GetProperVariableName(rel.SchemaName) + "_N1";
            }

            relationshipOneToMany.LogicalName = rel.ReferencingAttribute ?? relationshipOneToMany.LogicalName;

            relationshipOneToMany.EntityRole = "null";
            relationshipOneToMany.Type       = Naming.GetProperVariableName(rel.ReferencedEntity);

            relationshipOneToMany.MetadataId = rel.MetadataId;


            if (rel.ReferencedEntity != null && rel.ReferencingEntity != null &&
                rel.ReferencedEntity == rel.ReferencingEntity)
            {
                relationshipOneToMany.DisplayName = "Referencing" + relationshipOneToMany.DisplayName;
                relationshipOneToMany.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referencing";
            }
        }
        public static MappingRelationshipN1 Parse(OneToManyRelationshipMetadata rel, MappingField[] properties)
        {
            var property = properties.First(p => p.Attribute.LogicalName.ToLower() == rel.ReferencingAttribute.ToLower());

            var propertyName = property.DisplayName;

            var result = new MappingRelationshipN1
            {
                Attribute = new CrmRelationshipAttribute
                {
                    ToEntity           = rel.ReferencedEntity,
                    ToKey              = rel.ReferencedAttribute,
                    FromEntity         = rel.ReferencingEntity,
                    FromKey            = rel.ReferencingAttribute,
                    IntersectingEntity = ""
                },
                DisplayName = Naming.GetProperVariableName(rel.SchemaName),
                SchemaName  = Naming.GetProperVariableName(rel.SchemaName),
                LogicalName = rel.ReferencingAttribute,
                HybridName  = Naming.GetProperVariableName(rel.SchemaName) + "_N1",
                PrivateName = "_n1" + Naming.GetEntityPropertyPrivateName(rel.SchemaName),
                ForeignKey  = propertyName,
                Type        = Naming.GetProperVariableName(rel.ReferencedEntity),
                Property    = property,
                EntityRole  = "null",
                MetadataId  = rel.MetadataId
            };

            if (rel.ReferencedEntity == rel.ReferencingEntity)
            {
                result.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referencing";
                result.DisplayName = "Referencing" + result.DisplayName;
            }

            return(result);
        }
コード例 #4
0
        private static MappingEntity GetMappingEntity(EntityMetadata entityMetadata, string serverStamp)
        {
            var entity = new MappingEntity();

            entity.MetadataId  = entityMetadata.MetadataId;
            entity.ServerStamp = serverStamp;

            entity.Attribute             = new CrmEntityAttribute();
            entity.TypeCode              = entityMetadata.ObjectTypeCode;
            entity.Attribute.LogicalName = entityMetadata.LogicalName;
            entity.IsIntersect           = entityMetadata.IsIntersect ?? false;
            entity.Attribute.PrimaryKey  = entityMetadata.PrimaryIdAttribute;

            // entity.DisplayName = Helper.GetProperVariableName(entityMetadata.SchemaName);
            if (entityMetadata.DisplayName != null)
            {
                entity.Label = entityMetadata.DisplayName.UserLocalizedLabel != null
                                        ? entityMetadata.DisplayName.UserLocalizedLabel.Label
                                        : entityMetadata.SchemaName;
            }

            entity.SchemaName  = entityMetadata.SchemaName;
            entity.DisplayName = Naming.GetProperEntityName(entityMetadata.SchemaName);
            entity.HybridName  = Naming.GetProperHybridName(entityMetadata.SchemaName, entityMetadata.LogicalName);
            entity.StateName   = entity.HybridName + "State";

            if (entityMetadata.Description != null)
            {
                if (entityMetadata.Description.UserLocalizedLabel != null)
                {
                    entity.Description = entityMetadata.Description.UserLocalizedLabel.Label;
                }
            }

            var fields = entityMetadata.Attributes
                         .Where(a => a.AttributeOf == null)
                         .Select(a => MappingField.GetMappingField(a, entity)).ToList();

            fields.ForEach(f =>
            {
                if (f.DisplayName == entity.DisplayName)
                {
                    f.DisplayName += "1";
                }

                if (f.HybridName == entity.HybridName)
                {
                    f.HybridName += "1";
                }
            }
                           );

            AddEntityImageCrm2013(fields);
            AddLookupFields(fields);

            entity.Fields = fields.ToArray();
            entity.States =
                entityMetadata.Attributes.Where(a => a is StateAttributeMetadata)
                .Select(a => MappingEnum.GetMappingEnum(a as EnumAttributeMetadata))
                .FirstOrDefault();
            entity.Enums = entityMetadata.Attributes
                           .Where(a => a is PicklistAttributeMetadata || a is StateAttributeMetadata || a is StatusAttributeMetadata)
                           .Select(a => MappingEnum.GetMappingEnum(a as EnumAttributeMetadata)).ToArray();

            entity.PrimaryKey           = entity.Fields.First(f => f.Attribute.LogicalName == entity.Attribute.PrimaryKey);
            entity.PrimaryKeyProperty   = entity.PrimaryKey.DisplayName;
            entity.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute;

            entity.RelationshipsOneToMany = entityMetadata.OneToManyRelationships.Select(r =>
                                                                                         MappingRelationship1N.Parse(r,
                                                                                                                     entity.Fields)).ToArray();

            entity.RelationshipsManyToOne = entityMetadata.ManyToOneRelationships.Select(r =>
                                                                                         MappingRelationshipN1.Parse(r,
                                                                                                                     entity.Fields)).ToArray();

            var relationshipsManyToMany =
                entityMetadata.ManyToManyRelationships.Select(r => MappingRelationshipMN.Parse(r, entity.LogicalName)).ToList();
            var selfReferenced = relationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList();

            foreach (var referenced in selfReferenced)
            {
                var referencing = (MappingRelationshipMN)referenced.Clone();
                referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referenced.SchemaName);
                referencing.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referencing";
                relationshipsManyToMany.Add(referencing);
            }

            entity.RelationshipsManyToMany = relationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray();

            return(entity);
        }
コード例 #5
0
        private static void UpdateMappingEntity(EntityMetadata entityMetadata, MappingEntity entity, string serverStamp)
        {
            entity.ServerStamp = serverStamp;

            entity.TypeCode = entityMetadata.ObjectTypeCode ?? entity.TypeCode;
            entity.Attribute.LogicalName = entityMetadata.LogicalName ?? entity.Attribute.LogicalName;
            entity.IsIntersect           = (entityMetadata.IsIntersect ?? entity.IsIntersect);
            entity.Attribute.PrimaryKey  = entityMetadata.PrimaryIdAttribute ?? entity.Attribute.PrimaryKey;

            // entity.DisplayName = Helper.GetProperVariableName(entityMetadata.SchemaName);
            if (entityMetadata.DisplayName != null)
            {
                entity.Label = entityMetadata.DisplayName.UserLocalizedLabel != null
                                        ? entityMetadata.DisplayName.UserLocalizedLabel.Label
                                        : entityMetadata.SchemaName;
            }

            if (entityMetadata.SchemaName != null)
            {
                entity.DisplayName = Naming.GetProperEntityName(entityMetadata.SchemaName);
                entity.SchemaName  = entityMetadata.SchemaName;

                if (entityMetadata.LogicalName != null)
                {
                    entity.HybridName = Naming.GetProperHybridName(entityMetadata.SchemaName, entityMetadata.LogicalName);
                }
            }

            entity.StateName = entity.HybridName + "State";

            if (entityMetadata.Description != null)
            {
                if (entityMetadata.Description.UserLocalizedLabel != null)
                {
                    entity.Description = entityMetadata.Description.UserLocalizedLabel.Label;
                }
            }

            MappingField.UpdateCache(entityMetadata.Attributes.ToList(), entity);

            var fields = entity.Fields.ToList();

            fields.ForEach(f =>
            {
                if (f.DisplayName == entity.DisplayName)
                {
                    f.DisplayName += "1";
                }

                if (f.HybridName == entity.HybridName)
                {
                    f.HybridName += "1";
                }
            });

            AddEntityImageCrm2013(fields);
            AddLookupFields(fields);

            entity.Fields = fields.ToArray();
            var states = entity.Fields.FirstOrDefault(a => a.IsStateCode);

            entity.States = (states != null) ? states.EnumData : null;
            entity.Enums  = entity.Fields
                            .Where(a => a.EnumData != null)
                            .Select(a => a.EnumData).ToArray();

            entity.PrimaryKey           = entity.Fields.First(f => f.Attribute.LogicalName == entity.Attribute.PrimaryKey);
            entity.PrimaryKeyProperty   = entity.PrimaryKey.DisplayName;
            entity.PrimaryNameAttribute = entityMetadata.PrimaryNameAttribute ?? entity.PrimaryNameAttribute;

            MappingRelationship1N.UpdateCache(entityMetadata.OneToManyRelationships.ToList(), entity, entity.Fields);
            MappingRelationshipN1.UpdateCache(entityMetadata.ManyToOneRelationships.ToList(), entity, entity.Fields);
            MappingRelationshipMN.UpdateCache(entityMetadata.ManyToManyRelationships.ToList(), entity, entity.LogicalName);

            var relationshipsManyToMany = entity.RelationshipsManyToMany.ToList();
            var selfReferenced          = entity.RelationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList();

            foreach (var referenced in selfReferenced)
            {
                var referencing = (MappingRelationshipMN)referenced.Clone();
                referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referenced.SchemaName);
                referencing.EntityRole  = "Microsoft.Xrm.Sdk.EntityRole.Referencing";
                relationshipsManyToMany.Add(referencing);
            }

            entity.RelationshipsManyToMany = relationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray();
        }