예제 #1
0
        protected override List <MigrationOperation> GetOperations()
        {
            List <MigrationOperation> operations = new List <MigrationOperation>();

            #region operations
            if (Indexes != null)
            {
                foreach (var index in Indexes)
                {
                    var _DropIndexOperation = new DropIndexOperation();
                    _DropIndexOperation.Table = this.Table.Name.ToLower();
                    _DropIndexOperation.Name  = EF_CreateTable_Action.GetIndexName(index.Keys.Split(','), this.Table.id.Value);
                    operations.Add(_DropIndexOperation);
                }
            }

            var _DropTableOperation = new DropTableOperation()
            {
                Name = this.Table.Name.ToLower()
            };
            operations.Add(_DropTableOperation);
            #endregion
            return(operations);
        }
예제 #2
0
        protected override List <MigrationOperation> GetOperations()
        {
            List <MigrationOperation> operations = new List <MigrationOperation>();

            if (this.DeletedIndexes != null)
            {
                foreach (var index in DeletedIndexes)
                {
                    var _DropIndexOperation = new DropIndexOperation();
                    _DropIndexOperation.Table = this.OldTableName.ToLower();
                    _DropIndexOperation.Name  = EF_CreateTable_Action.GetIndexName(index.Keys.Split(','), this.TableId);
                    operations.Add(_DropIndexOperation);
                }
            }

            if (this.DeletedColumns != null)
            {
                foreach (var column in this.DeletedColumns)
                {
                    var _DropColumnOperation = new DropColumnOperation()
                    {
                        Name  = column.Name,
                        Table = this.OldTableName.ToLower(),
                    };
                    operations.Add(_DropColumnOperation);
                }
            }

            if (this.ChangedColumns != null)
            {
                foreach (var column in this.ChangedColumns)
                {
                    if (column.ChangedProperties["Name"] != null)
                    {
                        var _RenameColumnOperation = new RenameColumnOperation()
                        {
                            Name    = column.ChangedProperties["Name"].OriginalValue.ToString().ToLower(),
                            NewName = column.Name.ToLower(),
                            Table   = this.OldTableName.ToLower(),
                        };
                        operations.Add(_RenameColumnOperation);
                    }
                    if (column.ChangedProperties["IsAutoIncrement"] != null)
                    {
                        var original = (bool)column.ChangedProperties["IsAutoIncrement"].OriginalValue;
                        if (original)
                        {
                            var _DropSequenceOperation = new DropSequenceOperation()
                            {
                                Name = column.Name,
                            };
                            operations.Add(_DropSequenceOperation);
                        }
                        else
                        {
                            var _CreateSequenceOperation = new CreateSequenceOperation()
                            {
                                StartValue = 1,
                                Name       = column.Name,
                            };
                            operations.Add(_CreateSequenceOperation);
                        }
                    }
                    if (column.ChangedProperties["IsPKID"] != null)
                    {
                        var original = (bool)column.ChangedProperties["IsPKID"].OriginalValue;
                        if (original)
                        {
                            var _DropPrimaryKeyOperation = new DropPrimaryKeyOperation()
                            {
                                Name  = column.Name,
                                Table = this.OldTableName.ToLower(),
                            };
                            operations.Add(_DropPrimaryKeyOperation);
                        }
                        else
                        {
                            var _AddPrimaryKeyOperation = new AddPrimaryKeyOperation()
                            {
                                Columns = new string[] { column.Name },
                                Table   = this.OldTableName.ToLower(),
                            };
                            operations.Add(_AddPrimaryKeyOperation);
                        }
                    }
                    if (column.ChangedProperties.Any(m => m.Key != "Name" && m.Key != "IsAutoIncrement" && m.Key != "IsPKID") == false)
                    {
                        continue;
                    }


                    var olddbtype = column.dbType;
                    if (column.ChangedProperties["dbType"] != null)
                    {
                        olddbtype = column.ChangedProperties["dbType"].OriginalValue.ToString();
                    }
                    var oldDefaultValue = column.defaultValue;
                    if (column.ChangedProperties["defaultValue"] != null)
                    {
                        oldDefaultValue = column.ChangedProperties["defaultValue"].OriginalValue.ToString();
                    }
                    string oldComputedColumnSql = null;
                    var    oldlength            = column.length;
                    if (column.ChangedProperties["length"] != null)
                    {
                        oldlength = column.ChangedProperties["length"].OriginalValue.ToString();
                    }
                    if (!string.IsNullOrEmpty(oldlength))
                    {
                        //借用ComputedColumnSql字段存放length
                        oldComputedColumnSql = oldlength;
                    }

                    var _AlterColumnOperation = new AlterColumnOperation()
                    {
                        Table        = this.OldTableName.ToLower(),
                        ClrType      = EF_CreateTable_Action.GetCSharpType(column.dbType),
                        ColumnType   = column.dbType,
                        DefaultValue = column.defaultValue,
                        IsNullable   = column.CanNull.GetValueOrDefault(),
                        Name         = column.Name.ToLower(),
                        OldColumn    = new ColumnOperation()
                        {
                            ClrType           = EF_CreateTable_Action.GetCSharpType(olddbtype),
                            ColumnType        = olddbtype,
                            DefaultValue      = oldDefaultValue,
                            ComputedColumnSql = oldComputedColumnSql,
                        },
                    };
                    if (!string.IsNullOrEmpty(column.length))
                    {
                        //借用ComputedColumnSql字段存放length
                        _AlterColumnOperation.ComputedColumnSql = column.length;
                    }
                    operations.Add(_AlterColumnOperation);
                }
            }

            if (this.NewTableName.ToLower() != this.OldTableName.ToLower())
            {
                var _RenameTableOperation = new RenameTableOperation()
                {
                    Name    = OldTableName.ToLower(),
                    NewName = NewTableName.ToLower(),
                };
                operations.Add(_RenameTableOperation);
            }

            if (this.NewColumns != null)
            {
                foreach (var column in this.NewColumns)
                {
                    var _AddColumnOperation = new AddColumnOperation()
                    {
                        Table        = this.NewTableName.ToLower(),
                        ClrType      = EF_CreateTable_Action.GetCSharpType(column.dbType),
                        ColumnType   = column.dbType,
                        DefaultValue = column.defaultValue,
                        IsUnicode    = true,
                        IsNullable   = column.CanNull.GetValueOrDefault(),
                        Name         = column.Name.ToLower(),
                    };
                    if (!string.IsNullOrEmpty(column.length))
                    {
                        //借用ComputedColumnSql字段存放length
                        _AddColumnOperation.ComputedColumnSql = column.length;
                    }
                    operations.Add(_AddColumnOperation);
                }
            }

            if (this.NewIndexes != null)
            {
                foreach (var indexCfg in this.NewIndexes)
                {
                    var keynames = indexCfg.Keys.Split(',');
                    var _CreateIndexOperation = new CreateIndexOperation();
                    _CreateIndexOperation.Table    = this.NewTableName.ToLower();
                    _CreateIndexOperation.Name     = EF_CreateTable_Action.GetIndexName(keynames, this.TableId);
                    _CreateIndexOperation.Columns  = keynames.Select(m => m.ToLower()).ToArray();
                    _CreateIndexOperation.IsUnique = indexCfg.IsUnique.GetValueOrDefault();

                    operations.Add(_CreateIndexOperation);
                }
            }

            return(operations);
        }