/// <summary>
        ///
        /// </summary>
        /// <param name="foreignKey"></param>
        /// <param name="foreignKeyMetadata"></param>
        /// <returns></returns>
        private ColumnInfo GetReferenceColumn(ForeignKeyInfo foreignKey, ForeignKeyMetadata foreignKeyMetadata)
        {
            var referenceColumn = foreignKeyMetadata.ReferencedColumn;

            if (string.IsNullOrEmpty(referenceColumn))
            {
                return(foreignKey?.ReferenceTable?.PrimaryKey?.PrimaryColumn);
            }

            return(foreignKey.ReferenceTable.GetColumn(referenceColumn));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="table"></param>
        /// <param name="foreignKeyMetadata"></param>
        /// <returns></returns>
        private ForeignKeyInfo GetForeignKey(MetadataTableInfo table, ForeignKeyMetadata foreignKeyMetadata)
        {
            var foreignKey = new ForeignKeyInfo();

            foreignKey.Name   = foreignKeyMetadata.Name;
            foreignKey.Column = table.GetColumn(foreignKeyMetadata.Column.Name);
            foreignKey.Table  = table;

            foreignKey.ReferenceTable  = GetReferenceTable(foreignKeyMetadata);
            foreignKey.ReferenceColumn = GetReferenceColumn(foreignKey, foreignKeyMetadata);

            MigrateManager.CorrectName(foreignKey);
            return(foreignKey);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="foreignKeyMetadata"></param>
        /// <returns></returns>
        private TableInfo GetReferenceTable(ForeignKeyMetadata foreignKeyMetadata)
        {
            var referencedTable = foreignKeyMetadata.ReferencedTable;
            var tableInfo       = _metadataTableInfos.FirstOrDefault(f => f.TableMetadata.TableType == referencedTable);

            if (tableInfo != null)
            {
                return(tableInfo);
            }

            var tableMetadata = MetadataManager.GetTableMetadata(referencedTable);

            return(GetTableInfo(tableMetadata));
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="columnMetadata"></param>
        /// <returns></returns>
        protected virtual ForeignKeyMetadata GetForeignKey(ColumnMetadata columnMetadata)
        {
            var attr = columnMetadata.Property.GetCustomAttribute <ForeignKeyAttribute>();

            if (attr == null)
            {
                return(null);
            }

            var foreignKey = new ForeignKeyMetadata();

            foreignKey.Column           = columnMetadata;
            foreignKey.Name             = attr.Name;
            foreignKey.Table            = columnMetadata.Table;
            foreignKey.ReferencedTable  = attr.ReferencedTable ?? columnMetadata.Property.PropertyType;
            foreignKey.ReferencedColumn = attr.ReferenceColumn ?? GetTableMetadata(foreignKey.ReferencedTable)?.PrimaryKey?.PrimaryColumn?.Name;

            return(foreignKey);
        }