public static MappingEntity Parse(EntityMetadata entityMetadata) { var entity = new MappingEntity(); entity.Attribute = new CrmEntityAttribute(); entity.TypeCode = entityMetadata.ObjectTypeCode; entity.Attribute.LogicalName = entityMetadata.LogicalName; entity.IsIntersect = (bool)entityMetadata.IsIntersect; entity.Attribute.PrimaryKey = entityMetadata.PrimaryIdAttribute; // entity.DisplayName = Helper.GetProperVariableName(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.Parse(a, entity)).ToList(); fields.ForEach(f => { if (f.DisplayName == entity.DisplayName) { f.DisplayName += "1"; } //f.HybridName = Naming.GetProperHybridFieldName(f.DisplayName, f.Attribute); } ); AddEnityImageCRM2013(fields); AddLookupFields(fields); entity.Fields = fields.ToArray(); entity.States = entityMetadata.Attributes.Where(a => a is StateAttributeMetadata).Select(a => MappingEnum.Parse(a as EnumAttributeMetadata)).FirstOrDefault(); entity.Enums = entityMetadata.Attributes .Where(a => a is PicklistAttributeMetadata || a is StateAttributeMetadata || a is StatusAttributeMetadata || a is BooleanAttributeMetadata) .Select(a => MappingEnum.Parse(a)).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.RelationshipsOneToMany.ToList().ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); entity.RelationshipsManyToOne = entityMetadata.ManyToOneRelationships.Select(r => MappingRelationshipN1.Parse(r, entity.Fields)).ToArray(); entity.RelationshipsManyToOne.ToList().ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); var RelationshipsManyToMany = entityMetadata.ManyToManyRelationships.Select(r => MappingRelationshipMN.Parse(r, entity.LogicalName)).ToList(); var selfReferenced = RelationshipsManyToMany.Where(r => r.IsSelfReferenced).ToList(); foreach (var referecned in selfReferenced) { var referencing = (MappingRelationshipMN)referecned.Clone(); referencing.DisplayName = "Referencing" + Naming.GetProperVariableName(referecned.SchemaName); referencing.EntityRole = "Microsoft.Xrm.Sdk.EntityRole.Referencing"; RelationshipsManyToMany.Add(referencing); } RelationshipsManyToMany.ForEach(r => { var newName = r.DisplayName; if (newName == entity.DisplayName) { newName = r.DisplayName += "1"; } if (entity.Fields.Any(e => e.DisplayName == newName)) { newName = r.DisplayName += "2"; } }); entity.RelationshipsManyToMany = RelationshipsManyToMany.OrderBy(r => r.DisplayName).ToArray(); return(entity); }