private static void CopyForeignKeyConstraint(
            DbDatabaseMetadata database, DbTableMetadata toTable,
            DbForeignKeyConstraintMetadata fk)
        {
            Contract.Requires(toTable != null);
            Contract.Requires(fk != null);

            var newFk = new DbForeignKeyConstraintMetadata
                {
                    DeleteAction = fk.DeleteAction,
                    Name =
                        database.Schemas.Single().Tables.SelectMany(t => t.ForeignKeyConstraints).
                            UniquifyName(fk.Name),
                    PrincipalTable = fk.PrincipalTable
                };

            // Make sure all the dependent columns refer to columns in the newTable
            SetAllDependentColumns(newFk, fk.DependentColumns, toTable.Columns);

            if (!toTable.ContainsEquivalentForeignKey(newFk))
            {
                toTable.ForeignKeyConstraints.Add(newFk);
            }
        }
        /// <summary>
        ///     Moves a foreign key constraint from oldTable to newTable and updates column references
        /// </summary>
        private static void MoveForeignKeyConstraint(
            DbTableMetadata fromTable, DbTableMetadata toTable, DbForeignKeyConstraintMetadata fk)
        {
            Contract.Requires(fromTable != null);
            Contract.Requires(toTable != null);
            Contract.Requires(fk != null);

            fromTable.ForeignKeyConstraints.Remove(fk);

            // Only move it to the new table if the destination is not the principal table or if all dependent columns are not FKs
            // Otherwise you end up with an FK from the PKs to the PKs of the same table
            if (fk.PrincipalTable != toTable
                ||
                !fk.DependentColumns.All(c => c.IsPrimaryKeyColumn))
            {
                // Make sure all the dependent columns refer to columns in the newTable
                var oldColumns = fk.DependentColumns.ToArray();
                fk.DependentColumns.Clear();
                SetAllDependentColumns(fk, oldColumns, toTable.Columns);

                if (!toTable.ContainsEquivalentForeignKey(fk))
                {
                    toTable.ForeignKeyConstraints.Add(fk);
                }
            }
        }