Beispiel #1
0
        public void GenerateOpertions(DatabaseChanges dbChanges)
        {
            this._operations.Clear();

            switch (dbChanges.ChangeType)
            {
                case ChangeType.Added:
                    this.CreateDatabase(dbChanges);
                    break;
                case ChangeType.Removed:
                    this.DropDatabase(dbChanges);
                    break;
                case ChangeType.Modified:
                    //为了保证外键的变化与表的变化不冲突,按照以下顺序生成操作:添加的表、修改的表(外键)、删除的表。
                    foreach (var item in dbChanges.TablesChanged.Where(t => t.ChangeType == ChangeType.Added))
                    {
                        this.GenerateOpertions(item);
                    }
                    foreach (var item in dbChanges.TablesChanged.Where(t => t.ChangeType == ChangeType.Modified))
                    {
                        this.GenerateOpertions(item);
                    }
                    foreach (var item in dbChanges.TablesChanged.Where(t => t.ChangeType == ChangeType.Removed))
                    {
                        this.GenerateOpertions(item);
                    }
                    break;
                default:
                    break;
            }

            foreach (var action in this._relationActions) { action(); }
            this._relationActions.Clear();
        }
Beispiel #2
0
        private Result AutoMigrate(DatabaseChanges changeSet, DateTime? maxTime = null)
        {
            //生成所有自动迁移操作
            var auto = new AutomationMigration() { Context = this };
            auto.GenerateOpertions(changeSet);
            var autoMigrations = auto.Operations;

            if (autoMigrations.Count > 0)
            {
                this.GenerateTimeId(autoMigrations, maxTime);

                return this.MigrateUpBatch(autoMigrations);
            }

            return true;
        }
Beispiel #3
0
        private void DropDatabase(DatabaseChanges dbChanges)
        {
            //反向按表间的引用关系删除表。
            var tables = dbChanges.OldDatabase.Tables;
            for (int i = tables.Count - 1; i >= 0; i--)
            {
                if (!dbChanges.NewDatabase.IsIgnored(tables[i].Name))
                {
                    this.RemoveTable(tables[i]);
                }
            }

            //当版本号嵌入到当前数据库中时,也不支持自动 DropDatabase。
            if (!Context.DbVersionProvider.IsEmbaded())
            {
                this.AddOperation(new DropDatabase { Database = dbChanges.OldDatabase.Name });
            }
        }
Beispiel #4
0
        private void CreateDatabase(DatabaseChanges dbChanges)
        {
            this.AddOperation(new CreateDatabase { Database = dbChanges.NewDatabase.Name });

            foreach (var table in dbChanges.NewDatabase.Tables)
            {
                if (!dbChanges.NewDatabase.IsIgnored(table.Name))
                {
                    this.AddTable(table);
                }
            }
        }