从现有架构创建架构?

问题描述

我有两个数据库

  1. 缺少索引、约束、外键等的数据库 A ...

  2. 具有所有缺失特征的数据库 B

我已经在我的 C# 代码中加载了两个数据库的架构,在比较它们之后,我将缺少的功能添加到第一个架构中。 现在我想将该架构更新回 sql Server。怎么做?我可以从现有架构创建架构吗?

这是我尝试过的:

public static void Compare(ref DbMetaData dbSchema,ref DbMetaData dbSchema2)
{

    foreach (var schemaGroup in dbSchema.SchemasDic.Values )
    {
        DbUserSchemaInfo schemaGroup2=dbSchema2.SchemasDic.Values.First(sc => sc.SchemaName == schemaGroup.SchemaName);

        foreach (var x in schemaGroup.TablesDic)
        {
            var y = schemaGroup2.TablesDic.First(sc => sc.Value.TableName == x.Value.TableName);
            // GET the difference in columns
            var colDiff = x.Value.Columns.Except(y.Value.Columns);
            // GET the difference in indexes
            var indexDiff = x.Value.Indexes.Except(y.Value.Indexes);
            // GET the difference in constraints
            var constraintDiff = x.Value.DfConstraints.Except(y.Value.DfConstraints);
            // GET the difference in Fk
            var foreignKeysDiff = x.Value.ForeignKeys.Except(y.Value.ForeignKeys);
            //check if a primary key exists
            var primaryKeysDiff = y.Value.PrimaryKey == null ? x.Value.PrimaryKey : null;
            // GET the difference in triggers
           var triggersDiff = x.Value.Triggers.Except(y.Value.Triggers);

            //merge the difference
            if (indexDiff.Count() != 0)
            {
                foreach (var index in indexDiff)
                {
                    y.Value.AddTableIndex(index.Value);
                }
            }
            if (constraintDiff.Count() != 0)
            {
                foreach (var constraint in constraintDiff)
                {
                    y.Value.AddDefaultConstraint(constraint.Value);
                }
            }
            if (foreignKeysDiff.Count() != 0)
            {
                foreach (var foreignKey in foreignKeysDiff)
                {
                    y.Value.AddFk(foreignKey.Value);
                }
            }
            if (triggersDiff.Count() != 0)
            {
                foreach (var trigger in triggersDiff)
                {
                    y.Value.AddTableTrigger(trigger.Value);
                }
            }

        }
    }

    //Update DbSchema in sqlserver
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)