/// <summary>
        /// </summary>
        /// <param name="source"></param>
        /// <param name="foreignColumn"></param>
        /// <param name="foreignTableName"></param>
        /// <returns></returns>
        public static TableAssociation FromChildManyToMany(TableEntity source, IMemberColumnSchema foreignColumn, string foreignTableName)
        {
            TableAssociation association = null;

            //ASpNetUser -> AspNetUserInRole -> AspnetRole
            IEntity intermediaryEntity = EntityStore.Instance.GetEntity(foreignColumn.Table.FullName) ?? EntityStore.Instance.GetExcludedEntity(foreignColumn.Table.FullName);
            IEntity rightEntity        = EntityStore.Instance.GetEntity(foreignTableName);

            if (intermediaryEntity != null && rightEntity != null)
            {
                int leftIndex  = source.EntityKeyName.Equals(foreignColumn.Table.ForeignKeys[0].PrimaryKeyMemberColumns[0].Table.Name, StringComparison.OrdinalIgnoreCase) ? 0 : 1;
                int rightIndex = leftIndex == 1 ? 0 : 1;

                var intermediaryAssocation = new TableAssociation(foreignColumn.Table.ForeignKeys[rightIndex], AssociationType.ManyToMany, rightEntity, intermediaryEntity, false, isChildManyToMany: true, sourceManyToManyTable: source);
                association = new TableAssociation(foreignColumn.Table.ForeignKeys[leftIndex], AssociationType.ManyToMany, source, intermediaryEntity, true, intermediaryAssociation: intermediaryAssocation, isChildManyToMany: true);
                //End Association between AspNetUser -> ASpNetRole with Intermediary table as a property
            }

            if (association == null || association.Properties.Count <= 0 || String.IsNullOrEmpty(association.AssociationKey))
            {
                return(null);
            }

            return(association);
        }
        /// <summary>
        /// Override to populate the properties from the implemented association.
        /// </summary>
        protected override void LoadProperties()
        {
            if (AssociationType == AssociationType.ManyToMany)
            {
                // From Parent Many To Many
                if (!_isChildManyToMany)
                {
                    for (int index = 0; index < AssociationSource.PrimaryKeyMemberColumns.Count; index++)
                    {
                        IProperty foreignProperty = ForeignEntity.Properties.FirstOrDefault(x => x.KeyName == AssociationSource.PrimaryKeyMemberColumns[index].Name);
                        IProperty property        = Entity.Properties.FirstOrDefault(x => x.KeyName == AssociationSource.ForeignKeyMemberColumns[index].Name);

                        //This checks to see if one side of the association is ignored (ignored column name etc...).
                        if (property != null && foreignProperty != null)
                        {
                            AddAssociationProperty(property, foreignProperty);
                        }
                    }

                    return;
                }

                // From Child Many To Many
                if (IntermediaryAssociation != null)
                {
                    //association = new TableAssociation(AssociationSource, AssociationType.ManyToMany, source, intermediaryEntity, true, intermediaryAssociation: intermediaryAssocation);
                    for (int index = 0; index < AssociationSource.PrimaryKeyMemberColumns.Count; index++)
                    {
                        List <IProperty> properties      = Entity.EntityKeyName.Equals(AssociationSource.PrimaryKeyMemberColumns[index].Table.Name, StringComparison.OrdinalIgnoreCase) ? Entity.Properties : ForeignEntity.Properties;
                        IProperty        foreignProperty = ForeignEntity.Properties.FirstOrDefault(x => x.KeyName == AssociationSource.ForeignKeyMemberColumns[index].Name);
                        IProperty        property        = properties.FirstOrDefault(x => x.KeyName == AssociationSource.PrimaryKeyMemberColumns[index].Name);

                        //This checks to see if one side of the association is ignored (ignored column name etc...).
                        if (property != null && foreignProperty != null)
                        {
                            AddAssociationProperty(property, foreignProperty);
                        }
                    }
                }
                else if (_sourceManyToManyTable != null)
                {
                    //var intermediaryAssocation = new TableAssociation(AssociationSource, AssociationType.ManyToMany, Entity, ForeignEntity, false);
                    for (int index = 0; index < AssociationSource.PrimaryKeyMemberColumns.Count; index++)
                    {
                        List <IProperty> properties      = _sourceManyToManyTable.EntityKeyName.Equals(AssociationSource.PrimaryKeyMemberColumns[index].Table.Name, StringComparison.OrdinalIgnoreCase) ? _sourceManyToManyTable.Properties : Entity.Properties;
                        IProperty        foreignProperty = ForeignEntity.Properties.FirstOrDefault(x => x.KeyName == AssociationSource.ForeignKeyMemberColumns[index].Name);
                        IProperty        property        = properties.FirstOrDefault(x => x.KeyName == AssociationSource.PrimaryKeyMemberColumns[index].Name);

                        //This checks to see if one side of the association is ignored (ignored column name etc...).
                        if (property != null && foreignProperty != null)
                        {
                            AddAssociationProperty(property, foreignProperty);
                        }
                    }
                }

                return;
            }

            if (!IsParentEntity)
            {
                // Populate Parent Association Properties
                for (int index = 0; index < AssociationSource.ForeignKeyMemberColumns.Count; index++)
                {
                    IMemberColumnSchema column        = AssociationSource.ForeignKeyMemberColumns[index]; //Local Column.
                    IMemberColumnSchema foreignColumn = AssociationSource.PrimaryKeyMemberColumns[index];

                    //Check to see if the IProperty is a primary key column, if it is a primary key, it also needs to be a Foreign
                    if (!column.IsPrimaryKeyMember || (column.IsPrimaryKeyMember && column.IsForeignKeyMember))
                    {
                        //Find the Properties for the columns
                        IProperty associatedMember = ForeignEntity.Properties.FirstOrDefault(x => x.KeyName == foreignColumn.Name);
                        IProperty property         = Entity.Properties.FirstOrDefault(x => x.KeyName == column.Name);

                        //This checks to see if one side of the association is ignored (ignored column name etc...).
                        if (property != null && associatedMember != null)
                        {
                            AddAssociationProperty(property, associatedMember);
                        }
                    }
                }
            }
            else
            {
                // Populate Child Association Properties
                for (int index = 0; index < AssociationSource.ForeignKeyMemberColumns.Count; index++)
                {
                    IMemberColumnSchema column        = AssociationSource.PrimaryKeyMemberColumns[index]; //Local Column.
                    IMemberColumnSchema foreignColumn = AssociationSource.ForeignKeyMemberColumns[index];

                    //Find the Properties for the columns
                    IProperty associatedMember = ForeignEntity.Properties.FirstOrDefault(x => x.KeyName == foreignColumn.Name);
                    IProperty property         = Entity.Properties.FirstOrDefault(x => x.KeyName == column.Name);

                    //This checks to see if one side of the association is ignored (ignored column name etc...).
                    if (property != null && associatedMember != null)
                    {
                        AddAssociationProperty(property, associatedMember);
                    }
                }
            }
        }
 private static bool IsEnumSystemType(IMemberColumnSchema column)
 {
     return(column.NativeType.Equals("int", StringComparison.OrdinalIgnoreCase) || column.NativeType.Equals("bigint", StringComparison.OrdinalIgnoreCase) || column.NativeType.Equals("tinyint", StringComparison.OrdinalIgnoreCase) || column.NativeType.Equals("byte", StringComparison.OrdinalIgnoreCase) || column.NativeType.Equals("smallint", StringComparison.OrdinalIgnoreCase));
 }