0

同じ問題に直面していますが、これについて何か進展はありますか ?ピボットプロデューサーの使用

ご回答有難うございます、

編集:これは、すべての制約を削除するために使用されるコードです

    private static void RemoveCodeFluentConstraintsTable(IList<PivotRunnerConstraint> constraints, String connectionString)
    {

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            // Set up a command with the given query and associate
            // this with the current connection.
            using (SqlCommand cmd = new SqlCommand("SELECT tables.name as tableName, default_constraints.name as constraintName FROM sys.all_columns INNER JOIN sys.tables ON all_columns.object_id = tables.object_id INNER JOIN sys.schemas ON tables.schema_id = schemas.schema_id INNER JOIN sys.default_constraints ON all_columns.default_object_id = default_constraints.object_id", con))
            {

                foreach (PivotRunnerConstraint constraint in constraints)
                {
                    String tableName = constraint.ParentName;
                    String constraintName = constraint.Name;
                    if (tableName != null && constraintName != null)
                    {
                        SqlCommand cmdConstraint = new SqlCommand("ALTER TABLE [MySchema].[" + tableName + "] DROP CONSTRAINT [" + constraintName + "]", con);
                        cmdConstraint.ExecuteNonQuery();
                    }

                }
                //con.Close();
            }
        }
        return;
    }
4

1 に答える 1

0

生成された制約名の前にテーブル名を付けて、カスタム命名規則を使用することにしました。

 public class MyNamingConvention : FormatNamingConvention
    {
        public override string GetName(INamedObject obj, IDictionary context)
        {
            Column column = obj as Column;
            if (column != null && column.Table != null)
            {
                var name = context["name"] as string;
                if (name != null && (name.StartsWith("DF_")))
                {
                    return column.Table.Name + base.GetName(obj, context);
                }
            }
            return base.GetName(obj, context);
        }
    }

同時に、衝突を避けるために既存の制約も削除する必要がありました。

private static void RemoveCodeFluentConstraints(string connectionString)
    {

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();

            // Set up a command with the given query and associate
            // this with the current connection.
            using (SqlCommand cmd = new SqlCommand("SELECT c.name, t.name FROM sys.objects c, sys.objects t, sys.schemas s WHERE c.type IN('F', 'PK', 'FK', 'UQ', 'D') AND c.parent_object_id = t.object_id and t.SCHEMA_ID = s.schema_id AND t.type = 'U' AND s.name = 'MySchema' ORDER BY c.type", con))
            {
                using (IDataReader dr = cmd.ExecuteReader())
                {
                    using (SqlConnection con1 = new SqlConnection(connectionString))
                    {
                        con1.Open();
                        while (dr.Read())
                        {
                            String constraintName = dr[0].ToString();
                            String tableName = dr[1].ToString();
                            if (tableName != null && constraintName != null)
                            {
                                String cmdConstraintSql = "ALTER TABLE [MySchema].[" + tableName + "] DROP CONSTRAINT [" + constraintName + "]";
                                ActivityLog.Write("Execute " + cmdConstraintSql, ActivityLogsFile);
                                SqlCommand cmdConstraint = new SqlCommand(cmdConstraintSql, con1);
                                cmdConstraint.ExecuteNonQuery();
                            }

                        }
                        con1.Close();
                    }
                }
            }
            con.Close();
        }
        return;
    }

その他の問題は、ピボット ファイルの定義が正しく選択されていないことに関連していました: Pivot Runner null コマンド

于 2016-04-06T06:44:20.963 に答える