示例#1
0
        public virtual IEnumerable <MigrationOperation> GetUpgradeOperations()
        {
            if (this.Exists((string)null))
            {
                DbConnection connection = (DbConnection)null;
                try
                {
                    connection = this.CreateConnection();
                    string             tableName = "dbo.__MigrationHistory";
                    DbProviderManifest providerManifest;
                    if (connection.GetProviderInfo(out providerManifest).IsSqlCe())
                    {
                        tableName = "__MigrationHistory";
                    }
                    using (LegacyHistoryContext context = new LegacyHistoryContext(connection))
                    {
                        bool createdOnExists = false;
                        try
                        {
                            this.InjectInterceptionContext((DbContext)context);
                            using (new TransactionScope(TransactionScopeOption.Suppress))
                                context.History.Select <LegacyHistoryRow, DateTime>((Expression <Func <LegacyHistoryRow, DateTime> >)(h => h.CreatedOn)).FirstOrDefault <DateTime>();
                            createdOnExists = true;
                        }
                        catch (EntityException ex)
                        {
                        }
                        if (createdOnExists)
                        {
                            yield return((MigrationOperation) new DropColumnOperation(tableName, "CreatedOn", (object)null));
                        }
                    }
                    using (HistoryContext context = this.CreateContext(connection, (string)null))
                    {
                        if (!this._contextKeyColumnExists)
                        {
                            if (this._historyContextFactory != HistoryContext.DefaultFactory)
                            {
                                throw Error.UnableToUpgradeHistoryWhenCustomFactory();
                            }
                            string      table1       = tableName;
                            ColumnModel columnModel1 = new ColumnModel(PrimitiveTypeKind.String);
                            columnModel1.MaxLength    = new int?(this._contextKeyMaxLength);
                            columnModel1.Name         = "ContextKey";
                            columnModel1.IsNullable   = new bool?(false);
                            columnModel1.DefaultValue = (object)this._contextKey;
                            ColumnModel column1 = columnModel1;
                            yield return((MigrationOperation) new AddColumnOperation(table1, column1, (object)null));

                            XDocument               emptyModel           = new DbModelBuilder().Build(connection).GetModel();
                            CreateTableOperation    createTableOperation = (CreateTableOperation) new EdmModelDiffer().Diff(emptyModel, context.GetModel(), (Lazy <ModificationCommandTreeGenerator>)null, (MigrationSqlGenerator)null, (string)null, (string)null).Single <MigrationOperation>();
                            DropPrimaryKeyOperation primaryKeyOperation1 = new DropPrimaryKeyOperation((object)null);
                            primaryKeyOperation1.Table = tableName;
                            primaryKeyOperation1.CreateTableOperation = createTableOperation;
                            DropPrimaryKeyOperation dropPrimaryKeyOperation = primaryKeyOperation1;
                            dropPrimaryKeyOperation.Columns.Add("MigrationId");
                            yield return((MigrationOperation)dropPrimaryKeyOperation);

                            string      table2       = tableName;
                            ColumnModel columnModel2 = new ColumnModel(PrimitiveTypeKind.String);
                            columnModel2.MaxLength  = new int?(this._migrationIdMaxLength);
                            columnModel2.Name       = "MigrationId";
                            columnModel2.IsNullable = new bool?(false);
                            ColumnModel column2 = columnModel2;
                            yield return((MigrationOperation) new AlterColumnOperation(table2, column2, false, (object)null));

                            AddPrimaryKeyOperation primaryKeyOperation2 = new AddPrimaryKeyOperation((object)null);
                            primaryKeyOperation2.Table = tableName;
                            AddPrimaryKeyOperation addPrimaryKeyOperation = primaryKeyOperation2;
                            addPrimaryKeyOperation.Columns.Add("MigrationId");
                            addPrimaryKeyOperation.Columns.Add("ContextKey");
                            yield return((MigrationOperation)addPrimaryKeyOperation);
                        }
                    }
                }
                finally
                {
                    this.DisposeConnection(connection);
                }
            }
        }
        private static MigrationOperation[] Create_operations_to_upgrade_primary_key_of_history_table()
        {
            const string tableName = "dbo." + HistoryContext.DefaultTableName;

            CreateTableOperation createTableOperation;
            using (var context = new HistoryContext())
            {
                var emptyModel = new DbModelBuilder()
                    .Build(context.Database.Connection).GetModel();
                createTableOperation = (CreateTableOperation)
                                       new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();
            }

            var addColumnOperation =
                new AddColumnOperation(
                    tableName,
                    new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 512,
                            Name = "ContextKey",
                            IsNullable = false,
                            DefaultValue = "DefaultContextKey"
                        });

            var dropPrimaryKeyOperation
                = new DropPrimaryKeyOperation
                      {
                          Table = tableName,
                          CreateTableOperation = createTableOperation
                      };

            dropPrimaryKeyOperation.Columns.Add("MigrationId");

            var alterColumnOperation
                = new AlterColumnOperation(
                    tableName,
                    new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 150,
                            Name = "MigrationId",
                            IsNullable = false
                        },
                    isDestructiveChange: false);

            var addPrimaryKeyOperation
                = new AddPrimaryKeyOperation
                      {
                          Table = tableName
                      };

            addPrimaryKeyOperation.Columns.Add("MigrationId");
            addPrimaryKeyOperation.Columns.Add("ContextKey");

            return
                new MigrationOperation[]
                    {
                        addColumnOperation,
                        dropPrimaryKeyOperation,
                        alterColumnOperation,
                        addPrimaryKeyOperation
                    };
        }
示例#3
0
        private XDocument GetInitialHistoryModel()
        {
            var initialHistoryModel
                = (from migrationId in _migrationAssembly.MigrationIds
                   let migrationMetadata = (IMigrationMetadata)_migrationAssembly.GetMigration(migrationId)
                   select new ModelCompressor().Decompress(Convert.FromBase64String(migrationMetadata.Target)))
                    .FirstOrDefault();

            if (initialHistoryModel == null)
            {
                using (var historyContext = new HistoryContext(CreateConnection(), true, null))
                {
                    initialHistoryModel = historyContext.GetModel();

                    initialHistoryModel
                        .Descendants()
                        .Each(a => a.SetAttributeValue(EdmXNames.IsSystemName, true));
                }
            }

            return initialHistoryModel;
        }