예제 #1
0
        public virtual IEnumerable <MigrationOperation> GetUpgradeOperations()
        {
            if (!Exists())
            {
                yield break;
            }

            DbConnection connection = null;

            try
            {
                connection = CreateConnection();

                var tableName = "dbo." + HistoryContext.DefaultTableName;

                DbProviderManifest providerManifest;
                if (connection.GetProviderInfo(out providerManifest).IsSqlCe())
                {
                    tableName = HistoryContext.DefaultTableName;
                }

                using (var context = new LegacyHistoryContext(connection))
                {
                    var createdOnExists = false;

                    try
                    {
                        InjectInterceptionContext(context);

                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                            .Select(h => h.CreatedOn)
                            .FirstOrDefault();
                        }

                        createdOnExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (createdOnExists)
                    {
                        yield return(new DropColumnOperation(tableName, "CreatedOn"));
                    }
                }

                using (var context = CreateContext(connection))
                {
                    if (!_contextKeyColumnExists)
                    {
                        if (_historyContextFactory != HistoryContext.DefaultFactory)
                        {
                            throw Error.UnableToUpgradeHistoryWhenCustomFactory();
                        }

                        yield return(new AddColumnOperation(
                                         tableName,
                                         new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = _contextKeyMaxLength,
                            Name = "ContextKey",
                            IsNullable = false,
                            DefaultValue = _contextKey
                        }));

                        var emptyModel           = new DbModelBuilder().Build(connection).GetModel();
                        var createTableOperation = (CreateTableOperation)
                                                   new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();

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

                        dropPrimaryKeyOperation.Columns.Add("MigrationId");

                        yield return(dropPrimaryKeyOperation);

                        yield return(new AlterColumnOperation(
                                         tableName,
                                         new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = _migrationIdMaxLength,
                            Name = "MigrationId",
                            IsNullable = false
                        },
                                         isDestructiveChange: false));

                        var addPrimaryKeyOperation
                            = new AddPrimaryKeyOperation
                            {
                            Table = tableName
                            };

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

                        yield return(addPrimaryKeyOperation);
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
예제 #2
0
        public virtual IEnumerable <MigrationOperation> GetUpgradeOperations()
        {
            if (!Exists())
            {
                yield break;
            }

            using (var connection = CreateConnection())
            {
                const string tableName = "dbo." + HistoryContext.DefaultTableName;

                using (var context = CreateContext(connection))
                {
                    var productVersionExists = false;

                    try
                    {
                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                            .Select(h => h.ProductVersion)
                            .FirstOrDefault();
                        }

                        productVersionExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (!productVersionExists)
                    {
                        yield return(new DropColumnOperation(tableName, "Hash"));

                        yield return(new AddColumnOperation(
                                         tableName,
                                         new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 32,
                            Name = "ProductVersion",
                            IsNullable = false,
                            DefaultValue = "0.7.0.0"
                        }));
                    }

                    if (!_contextKeyColumnExists)
                    {
                        yield return(new AddColumnOperation(
                                         tableName,
                                         new ColumnModel(PrimitiveTypeKind.String)
                        {
                            MaxLength = 512,
                            Name = "ContextKey",
                            IsNullable = false,
                            DefaultValue = _contextKey
                        }));

                        var dropPrimaryKeyOperation
                            = new DropPrimaryKeyOperation
                            {
                            Table = tableName
                            };

                        dropPrimaryKeyOperation.Columns.Add("MigrationId");

                        yield return(dropPrimaryKeyOperation);

                        var addPrimaryKeyOperation
                            = new AddPrimaryKeyOperation
                            {
                            Table = tableName
                            };

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

                        yield return(addPrimaryKeyOperation);
                    }
                }

                using (var context = new LegacyHistoryContext(connection))
                {
                    var createdOnExists = false;

                    try
                    {
                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                            .Select(h => h.CreatedOn)
                            .FirstOrDefault();
                        }

                        createdOnExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (createdOnExists)
                    {
                        yield return(new DropColumnOperation(tableName, "CreatedOn"));
                    }
                }
            }
        }
예제 #3
0
        public virtual IEnumerable<MigrationOperation> GetUpgradeOperations()
        {
            if (!Exists())
            {
                yield break;
            }

            DbConnection connection = null;
            try
            {
                connection = CreateConnection();

                var tableName = "dbo." + HistoryContext.DefaultTableName;

                DbProviderManifest providerManifest;
                if (connection.GetProviderInfo(out providerManifest).IsSqlCe())
                {
                    tableName = HistoryContext.DefaultTableName;
                }

                using (var context = new LegacyHistoryContext(connection))
                {
                    var createdOnExists = false;

                    try
                    {
                        InjectInterceptionContext(context);

                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                                .Select(h => h.CreatedOn)
                                .FirstOrDefault();
                        }

                        createdOnExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (createdOnExists)
                    {
                        yield return new DropColumnOperation(tableName, "CreatedOn");
                    }
                }

                using (var context = CreateContext(connection))
                {
                    if (!_contextKeyColumnExists)
                    {
                        if (_historyContextFactory != HistoryContext.DefaultFactory)
                        {
                            throw Error.UnableToUpgradeHistoryWhenCustomFactory();
                        }

                        yield return new AddColumnOperation(
                            tableName,
                            new ColumnModel(PrimitiveTypeKind.String)
                                {
                                    MaxLength = _contextKeyMaxLength,
                                    Name = "ContextKey",
                                    IsNullable = false,
                                    DefaultValue = _contextKey
                                });

                        var emptyModel = new DbModelBuilder().Build(connection).GetModel();
                        var createTableOperation = (CreateTableOperation)
                            new EdmModelDiffer().Diff(emptyModel, context.GetModel()).Single();

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

                        dropPrimaryKeyOperation.Columns.Add("MigrationId");

                        yield return dropPrimaryKeyOperation;

                        yield return new AlterColumnOperation(
                            tableName,
                            new ColumnModel(PrimitiveTypeKind.String)
                                {
                                    MaxLength = _migrationIdMaxLength,
                                    Name = "MigrationId",
                                    IsNullable = false
                                },
                            isDestructiveChange: false);

                        var addPrimaryKeyOperation
                            = new AddPrimaryKeyOperation
                                {
                                    Table = tableName
                                };

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

                        yield return addPrimaryKeyOperation;
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
        private HistoryRepository SetupHistoryRepositoryForOrderingTest()
        {
            ResetDatabase();

            using (var context = CreateContext<ShopContext_v1>())
            {
                var model = context.GetModel();

                var clonedConnection = DbProviderServices.GetProviderFactory(context.Database.Connection).CreateConnection();
                clonedConnection.ConnectionString = context.Database.Connection.ConnectionString;

                using (var historyContext = new LegacyHistoryContext(clonedConnection))
                {
                    context.InternalContext.MarkDatabaseInitialized();

                    context.Database.ExecuteSqlCommand(
                        ((IObjectContextAdapter)historyContext).ObjectContext.CreateDatabaseScript());

                    historyContext.History.Add(
                        new HistoryRow
                            {
                                MigrationId = "227309030010001_Migration1",
#pragma warning disable 612,618
                                CreatedOn = new DateTime(2273, 9, 3, 0, 10, 0, 0, DateTimeKind.Utc),
#pragma warning restore 612,618
                                Model = new ModelCompressor().Compress(model),
                                ProductVersion = "",
                            });

                    historyContext.History.Add(
                        new HistoryRow
                            {
                                MigrationId = "227209030010001_Migration2", // Id is before
#pragma warning disable 612,618
                                CreatedOn = new DateTime(2274, 9, 3, 0, 10, 0, 0, DateTimeKind.Utc), // CreatedOn is after
#pragma warning restore 612,618
                                Model = new ModelCompressor().Compress(model),
                                ProductVersion = "",
                            });

                    historyContext.SaveChanges();
                }
            }

            return new HistoryRepository(ConnectionString, ProviderFactory);
        }
        public virtual IEnumerable<MigrationOperation> GetUpgradeOperations()
        {
            if (!Exists())
            {
                yield break;
            }

            using (var connection = CreateConnection())
            {
                const string tableName = "dbo." + HistoryContext.DefaultTableName;

                using (var context = CreateContext(connection))
                {
                    var productVersionExists = false;

                    try
                    {
                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                                   .Select(h => h.ProductVersion)
                                   .FirstOrDefault();
                        }

                        productVersionExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (!productVersionExists)
                    {
                        yield return new DropColumnOperation(tableName, "Hash");

                        yield return new AddColumnOperation(
                            tableName,
                            new ColumnModel(PrimitiveTypeKind.String)
                            {
                                MaxLength = 32,
                                Name = "ProductVersion",
                                IsNullable = false,
                                DefaultValue = "0.7.0.0"
                            });
                    }

                    if (!_contextKeyColumnExists)
                    {
                        yield return new AddColumnOperation(
                            tableName,
                            new ColumnModel(PrimitiveTypeKind.String)
                            {
                                MaxLength = 512,
                                Name = "ContextKey",
                                IsNullable = false,
                                DefaultValue = _contextKey
                            });

                        var dropPrimaryKeyOperation
                            = new DropPrimaryKeyOperation
                            {
                                Table = tableName
                            };

                        dropPrimaryKeyOperation.Columns.Add("MigrationId");

                        yield return dropPrimaryKeyOperation;

                        var addPrimaryKeyOperation
                            = new AddPrimaryKeyOperation
                            {
                                Table = tableName
                            };

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

                        yield return addPrimaryKeyOperation;
                    }
                }

                using (var context = new LegacyHistoryContext(connection))
                {
                    var createdOnExists = false;

                    try
                    {
                        using (new TransactionScope(TransactionScopeOption.Suppress))
                        {
                            context.History
                                   .Select(h => h.CreatedOn)
                                   .FirstOrDefault();
                        }

                        createdOnExists = true;
                    }
                    catch (EntityException)
                    {
                    }

                    if (createdOnExists)
                    {
                        yield return new DropColumnOperation(tableName, "CreatedOn");
                    }
                }
            }
        }
예제 #6
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);
                }
            }
        }