示例#1
0
        public override IEnumerable <string> SqlCommands(IDbConnection connection)
        {
            foreach (var cmd in base.SqlCommands(connection))
            {
                yield return(cmd);
            }

            CreateTable ct = new CreateTable(_modelType);
            string      pkName;
            bool        inPK = ct.InPrimaryKey(_columnRef.ColumnName, out pkName) || connection.IsColumnInPrimaryKey(_columnRef, out pkName);

            if (inPK)
            {
                yield return($"ALTER TABLE [{_columnRef.Schema}].[{_columnRef.TableName}] DROP CONSTRAINT [{pkName}]");
            }

            ForeignKeyRef fk;

            if (_columnRef.IsForeignKey(connection, out fk))
            {
                yield return($"ALTER TABLE [{_columnRef.Schema}].[{_columnRef.TableName}] DROP CONSTRAINT [{fk.ConstraintName}]");
            }

            yield return($"ALTER TABLE [{_columnRef.Schema}].[{_columnRef.TableName}] DROP COLUMN [{_columnRef.ColumnName}]");

            if (inPK)
            {
                yield return($"ALTER TABLE [{_columnRef.Schema}].[{_columnRef.TableName}] ADD {ct.CreateTablePrimaryKey(ct.GetClusterAttribute())}");
            }
        }
示例#2
0
        //private readonly IEnumerable<ForeignKeyRef> _foreignKeys; you won't be dropping the key usually, so there's really no need to drop dependent FKs

        internal DropColumn(ColumnRef columnRef, IDbConnection connection) : base(MergeObjectType.Column, MergeActionType.Delete, columnRef.ToString())
        {
            _columnRef = columnRef;

            ForeignKeyRef fk;

            if (columnRef.IsForeignKey(connection, out fk))
            {
                _dropFK = fk;
            }
            //_foreignKeys = GetReferencingForeignKeys(connection, columnRef.ObjectID);
            _cn = connection;
        }