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

            var modelType = _object.ModelType;

            var ct = new CreateTable(modelType);

            ClusterAttribute clusterAttribute =
                modelType.GetCustomAttribute <ClusterAttribute>() ??
                new ClusterAttribute(ClusterOption.PrimaryKey);

            yield return($"ALTER TABLE [{_object.Schema}].[{_object.Name}] ADD {ct.CreateTablePrimaryKey(clusterAttribute)}");
        }
Exemple #2
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())}");
            }
        }