Pivot Runner: force delete of existing constraint that match a constraint needed for the new model

廉价感情. 提交于 2019-12-12 06:45:18

问题


Facing same issue, is there any progress on this one : http://www.softfluent.com/product/codefluent-entities/knowledge-center/point-sql-server-producer-to-production-db-instead-of-using-pivot-producer

Thanks for your answer,

EDIT: this is the code used to delete all the constraints

    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;
    }

回答1:


I went for using a custom naming convention using table name prefixing the generated constraint name.

 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);
        }
    }

At the same time, I also had to delete existing constraints to avoid collision:

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;
    }

Other problem were related to definition of pivot file not being picked correctly: Pivot Runner null command



来源:https://stackoverflow.com/questions/36356375/pivot-runner-force-delete-of-existing-constraint-that-match-a-constraint-needed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!