0

面对同样的问题,这方面是否有任何进展:http: //www.softfluent.com/product/codefluent-entities/knowledge-center/point-sql-server-producer-to-production-db-instead-of-使用枢轴生产者

感谢您的回答,

编辑:这是用于删除所有约束的代码

    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 回答