public static void UpdateCache(List <ManyToManyRelationshipMetadata> relMetadataList, MappingEntity mappingEntity , string thisEntityLogicalName) { // update modified fields var modifiedRelations = mappingEntity.RelationshipsManyToMany .Where(relation => relMetadataList.Exists(relMeta => relMeta.MetadataId == relation.MetadataId)).ToList(); modifiedRelations.ForEach( rel => Update(relMetadataList.First(relMeta => relMeta.MetadataId == rel.MetadataId), rel, thisEntityLogicalName)); // add new attributes var newRelMeta = relMetadataList .Where( relMeta => !Array.Exists(mappingEntity.RelationshipsManyToMany, relation => relation.MetadataId == relMeta.MetadataId)) .ToList(); newRelMeta.ForEach( relMeta => { var newRelations = new MappingRelationshipMN[mappingEntity.RelationshipsManyToMany.Length + 1]; Array.Copy(mappingEntity.RelationshipsManyToMany, newRelations, mappingEntity.RelationshipsManyToMany.Length); mappingEntity.RelationshipsManyToMany = newRelations; mappingEntity.RelationshipsManyToMany[mappingEntity.RelationshipsManyToMany.Length - 1] = Parse(relMeta, thisEntityLogicalName); }); }
public static MappingRelationshipMN Parse(ManyToManyRelationshipMetadata rel, string thisEntityLogicalName) { var result = new MappingRelationshipMN(); if (rel.Entity1LogicalName == thisEntityLogicalName) { result.Attribute = new CrmRelationshipAttribute { FromEntity = rel.Entity1LogicalName, FromKey = rel.Entity1IntersectAttribute, ToEntity = rel.Entity2LogicalName, ToKey = rel.Entity2IntersectAttribute, IntersectingEntity = rel.IntersectEntityName }; } else { result.Attribute = new CrmRelationshipAttribute { ToEntity = rel.Entity1LogicalName, ToKey = rel.Entity1IntersectAttribute, FromEntity = rel.Entity2LogicalName, FromKey = rel.Entity2IntersectAttribute, IntersectingEntity = rel.IntersectEntityName }; } result.EntityRole = "null"; result.SchemaName = Naming.GetProperVariableName(rel.SchemaName); result.DisplayName = Naming.GetProperVariableName(rel.SchemaName); if (rel.Entity1LogicalName == rel.Entity2LogicalName && rel.Entity1LogicalName == thisEntityLogicalName) { result.DisplayName = "Referenced" + result.DisplayName; result.EntityRole = "Microsoft.Xrm.Sdk.EntityRole.Referenced"; result.IsSelfReferenced = true; } if (result.DisplayName == thisEntityLogicalName) { result.DisplayName += "1"; // this is what CrmSvcUtil does } result.HybridName = Naming.GetProperVariableName(rel.SchemaName) + "_NN"; result.PrivateName = "_nn" + Naming.GetEntityPropertyPrivateName(rel.SchemaName); result.ForeignKey = Naming.GetProperVariableName(result.Attribute.ToKey); result.Type = Naming.GetProperVariableName(result.Attribute.ToEntity); result.MetadataId = rel.MetadataId; return(result); }
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); }
public static void Update(ManyToManyRelationshipMetadata rel, MappingRelationshipMN relationshipManyToMany, string thisEntityLogicalName) { if (rel.Entity1LogicalName != null) { if (rel.Entity1LogicalName == thisEntityLogicalName) { relationshipManyToMany.Attribute.FromEntity = rel.Entity1LogicalName ?? relationshipManyToMany.Attribute.FromEntity; relationshipManyToMany.Attribute.FromKey = rel.Entity1IntersectAttribute ?? relationshipManyToMany.Attribute.FromKey; relationshipManyToMany.Attribute.ToEntity = rel.Entity2LogicalName ?? relationshipManyToMany.Attribute.ToEntity; relationshipManyToMany.Attribute.ToKey = rel.Entity2IntersectAttribute ?? relationshipManyToMany.Attribute.ToKey; relationshipManyToMany.Attribute.IntersectingEntity = rel.IntersectEntityName ?? relationshipManyToMany.Attribute.IntersectingEntity; } else { relationshipManyToMany.Attribute.ToEntity = rel.Entity1LogicalName ?? relationshipManyToMany.Attribute.ToEntity; relationshipManyToMany.Attribute.ToKey = rel.Entity1IntersectAttribute ?? relationshipManyToMany.Attribute.ToKey; relationshipManyToMany.Attribute.FromEntity = rel.Entity2LogicalName ?? relationshipManyToMany.Attribute.FromEntity; relationshipManyToMany.Attribute.FromKey = rel.Entity2IntersectAttribute ?? relationshipManyToMany.Attribute.FromKey; relationshipManyToMany.Attribute.IntersectingEntity = rel.IntersectEntityName ?? relationshipManyToMany.Attribute.IntersectingEntity; } } relationshipManyToMany.EntityRole = "null"; if (rel.SchemaName != null) { relationshipManyToMany.SchemaName = Naming.GetProperVariableName(rel.SchemaName); relationshipManyToMany.DisplayName = Naming.GetProperVariableName(rel.SchemaName); } if (rel.Entity1LogicalName != null && rel.Entity2LogicalName != null) { if (rel.Entity1LogicalName == rel.Entity2LogicalName && rel.Entity1LogicalName == thisEntityLogicalName) { relationshipManyToMany.DisplayName = "Referenced" + relationshipManyToMany.DisplayName; relationshipManyToMany.EntityRole = "Microsoft.Xrm.Sdk.EntityRole.Referenced"; relationshipManyToMany.IsSelfReferenced = true; } } if (relationshipManyToMany.DisplayName == thisEntityLogicalName) { relationshipManyToMany.DisplayName += "1"; // this is what CrmSvcUtil does } if (rel.SchemaName != null) { relationshipManyToMany.HybridName = Naming.GetProperVariableName(rel.SchemaName) + "_NN"; relationshipManyToMany.PrivateName = "_nn" + Naming.GetEntityPropertyPrivateName(rel.SchemaName); } relationshipManyToMany.ForeignKey = Naming.GetProperVariableName(relationshipManyToMany.Attribute.ToKey); relationshipManyToMany.Type = Naming.GetProperVariableName(relationshipManyToMany.Attribute.ToEntity); relationshipManyToMany.MetadataId = rel.MetadataId; }
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(); }