示例#1
0
        public virtual void BootstrapUsingEFProviderDdl(VersionedModel versionedModel)
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                using (HistoryContext context = this.CreateContext(connection, (string)null))
                {
                    context.Database.ExecuteSqlCommand(((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript());
                    context.History.Add(new HistoryRow()
                    {
                        MigrationId    = MigrationAssembly.CreateMigrationId(Strings.InitialCreate).RestrictTo(this._migrationIdMaxLength),
                        ContextKey     = this._contextKey,
                        Model          = new ModelCompressor().Compress(versionedModel.Model),
                        ProductVersion = versionedModel.Version ?? HistoryRepository._productVersion
                    });
                    context.SaveChanges();
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
示例#2
0
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                using (HistoryContext context = this.CreateContext(connection, (string)null))
                {
                    HistoryRow entity = new HistoryRow()
                    {
                        MigrationId = migrationId.RestrictTo(this._migrationIdMaxLength),
                        ContextKey  = this._contextKey
                    };
                    context.History.Attach(entity);
                    context.History.Remove(entity);
                    using (CommandTracer commandTracer = new CommandTracer((DbContext)context))
                    {
                        context.SaveChanges();
                        return((MigrationOperation) new HistoryOperation((IList <DbModificationCommandTree>)commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList <DbModificationCommandTree>(), (object)null));
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
示例#3
0
        public virtual MigrationOperation CreateInsertOperation(
            string migrationId,
            VersionedModel versionedModel)
        {
            DbConnection connection = (DbConnection)null;

            try
            {
                connection = this.CreateConnection();
                using (HistoryContext context = this.CreateContext(connection, (string)null))
                {
                    context.History.Add(new HistoryRow()
                    {
                        MigrationId    = migrationId.RestrictTo(this._migrationIdMaxLength),
                        ContextKey     = this._contextKey,
                        Model          = new ModelCompressor().Compress(versionedModel.Model),
                        ProductVersion = versionedModel.Version ?? HistoryRepository._productVersion
                    });
                    using (CommandTracer commandTracer = new CommandTracer((DbContext)context))
                    {
                        context.SaveChanges();
                        return((MigrationOperation) new HistoryOperation((IList <DbModificationCommandTree>)commandTracer.CommandTrees.OfType <DbModificationCommandTree>().ToList <DbModificationCommandTree>(), (object)null));
                    }
                }
            }
            finally
            {
                this.DisposeConnection(connection);
            }
        }
示例#4
0
        private HistoryRepository SetupHistoryRepositoryForOrderingTest()
        {
            ResetDatabase();

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

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

                using (var historyContext = new HistoryContext(clonedConnection, defaultSchema: null))
                {
                    context.InternalContext.MarkDatabaseInitialized();

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

                    historyContext.History.Add(
                        new HistoryRow
                    {
                        MigrationId    = "227309030010001_Migration1",
                        ContextKey     = "MyKey",
                        Model          = new ModelCompressor().Compress(model.Model),
                        ProductVersion = "",
                    });

                    historyContext.History.Add(
                        new HistoryRow
                    {
                        MigrationId    = "227209030010001_Migration2",
                        ContextKey     = "MyKey",
                        Model          = new ModelCompressor().Compress(model.Model),
                        ProductVersion = "",
                    });

                    historyContext.SaveChanges();
                }
            }

            return(new HistoryRepository(Mock.Of <InternalContextForMock>(), ConnectionString, ProviderFactory, "MyKey", null, HistoryContext.DefaultFactory));
        }
示例#5
0
        public void Generate_can_output_delete_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                var historyRow
                    = new HistoryRow
                          {
                              MigrationId = "House Lannister",
                              ContextKey = "The pointy end"
                          };

                historyContext.History.Attach(historyRow);
                historyContext.History.Remove(historyRow);

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var deleteHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { deleteHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"DELETE [__MigrationHistory]
WHERE (([MigrationId] = N'House Lannister') AND ([ContextKey] = N'The pointy end'))", sql.Sql.Trim());
                }
            }
        }
示例#6
0
        public void Generate_can_output_insert_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                historyContext.History.Add(
                    new HistoryRow
                        {
                            MigrationId = "House Lannister",
                            ContextKey = "The pointy end",
                            Model = new byte[0],
                            ProductVersion = "Awesomeness"
                        });

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var insertHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { insertHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"INSERT [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'House Lannister', N'The pointy end',  0x , N'Awesomeness')", sql.Sql.Trim());
                }
            }
        }
        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 HistoryContext(clonedConnection, defaultSchema: null))
                {
                    context.InternalContext.MarkDatabaseInitialized();

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

                    historyContext.History.Add(
                        new HistoryRow
                            {
                                MigrationId = "227309030010001_Migration1",
                                ContextKey = "MyKey",
                                Model = new ModelCompressor().Compress(model),
                                ProductVersion = "",
                            });

                    historyContext.History.Add(
                        new HistoryRow
                            {
                                MigrationId = "227209030010001_Migration2",
                                ContextKey = "MyKey",
                                Model = new ModelCompressor().Compress(model),
                                ProductVersion = "",
                            });

                    historyContext.SaveChanges();
                }
            }

            return new HistoryRepository(ConnectionString, ProviderFactory, "MyKey", null);
        }