示例#1
0
        public override object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
        {
            var baseType = typeDeclaration.BaseTypes.OfType <MemberType>().FirstOrDefault();

            if (baseType == null || baseType.MemberName != MappingBaseType)
            {
                return(base.VisitTypeDeclaration(typeDeclaration, data));
            }

            var entity = baseType.TypeArguments.OfType <MemberType>().FirstOrDefault();

            if (entity == null)
            {
                return(base.VisitTypeDeclaration(typeDeclaration, data));
            }

            if (ParsedEntity == null)
            {
                ParsedEntity = new ParsedEntity();
            }

            ParsedEntity.EntityClass  = entity.MemberName;
            ParsedEntity.MappingClass = typeDeclaration.Name;

            return(base.VisitTypeDeclaration(typeDeclaration, ParsedEntity));
        }
示例#2
0
        private static void UpdateRelationships(EntityContext generatedContext, Entity entity, ParsedEntity parsedEntity)
        {
            // sync relationships
            foreach (var parsedRelationship in parsedEntity.Relationships.Where(r => r.JoinTable == null))
            {
                var parsedProperties = parsedRelationship.ThisProperties;
                var relationship     = entity.Relationships
                                       .Where(r => !r.IsManyToMany)
                                       .FirstOrDefault(r => r.ThisProperties.Except(parsedProperties).Count() == 0);

                if (relationship == null)
                {
                    continue;
                }

                bool isThisSame  = relationship.ThisPropertyName == parsedRelationship.ThisPropertyName;
                bool isOtherSame = relationship.OtherPropertyName == parsedRelationship.OtherPropertyName;

                if (isThisSame && isOtherSame)
                {
                    continue;
                }

                if (!isThisSame)
                {
                    Debug.WriteLine("Rename Relationship Property '{0}.{1}' to '{0}.{2}'.",
                                    relationship.ThisEntity,
                                    relationship.ThisPropertyName,
                                    parsedRelationship.ThisPropertyName);

                    relationship.ThisPropertyName = parsedRelationship.ThisPropertyName;
                }
                if (!isOtherSame)
                {
                    Debug.WriteLine("Rename Relationship Property '{0}.{1}' to '{0}.{2}'.",
                                    relationship.OtherEntity,
                                    relationship.OtherPropertyName,
                                    parsedRelationship.OtherPropertyName);

                    relationship.OtherPropertyName = parsedRelationship.OtherPropertyName;
                }

                // sync other relationship
                var otherEntity = generatedContext.Entities.ByClass(relationship.OtherEntity);
                if (otherEntity == null)
                {
                    continue;
                }

                var otherRelationship = otherEntity.Relationships.ByName(relationship.RelationshipName);
                if (otherRelationship == null)
                {
                    continue;
                }

                otherRelationship.ThisPropertyName  = relationship.OtherPropertyName;
                otherRelationship.OtherPropertyName = relationship.ThisPropertyName;
            }

            // sync many to many
            foreach (var parsedRelationship in parsedEntity.Relationships.Where(r => r.JoinTable != null))
            {
                var joinThisColumn  = parsedRelationship.JoinThisColumn;
                var joinOtherColumn = parsedRelationship.JoinOtherColumn;

                var relationship = entity.Relationships
                                   .Where(r => r.IsManyToMany)
                                   .FirstOrDefault(r =>
                                                   r.JoinThisColumn.Except(joinThisColumn).Count() == 0 &&
                                                   r.JoinOtherColumn.Except(joinOtherColumn).Count() == 0 &&
                                                   r.JoinTable == parsedRelationship.JoinTable &&
                                                   r.JoinSchema == parsedRelationship.JoinSchema);

                if (relationship == null)
                {
                    continue;
                }


                bool isThisSame  = relationship.ThisPropertyName == parsedRelationship.ThisPropertyName;
                bool isOtherSame = relationship.OtherPropertyName == parsedRelationship.OtherPropertyName;

                if (isThisSame && isOtherSame)
                {
                    continue;
                }

                if (!isThisSame)
                {
                    Debug.WriteLine("Rename Relationship Property '{0}.{1}' to '{0}.{2}'.",
                                    relationship.ThisEntity,
                                    relationship.ThisPropertyName,
                                    parsedRelationship.ThisPropertyName);

                    relationship.ThisPropertyName = parsedRelationship.ThisPropertyName;
                }
                if (!isOtherSame)
                {
                    Debug.WriteLine("Rename Relationship Property '{0}.{1}' to '{0}.{2}'.",
                                    relationship.OtherEntity,
                                    relationship.OtherPropertyName,
                                    parsedRelationship.OtherPropertyName);

                    relationship.OtherPropertyName = parsedRelationship.OtherPropertyName;
                }

                // sync other relationship
                var otherEntity = generatedContext.Entities.ByClass(relationship.OtherEntity);
                if (otherEntity == null)
                {
                    continue;
                }

                var otherRelationship = otherEntity.Relationships.ByName(relationship.RelationshipName);
                if (otherRelationship == null)
                {
                    continue;
                }

                otherRelationship.ThisPropertyName  = relationship.OtherPropertyName;
                otherRelationship.OtherPropertyName = relationship.ThisPropertyName;
            }
        }