[Fact] // CodePlex 2029
        public void InitializeDatabase_does_not_create_new_connections()
        {
            using (var context = new SomeContext())
            {
                context.Database.Initialize(force: false);

                using (var transactionContext = new SomeTransactionContext(context.Database.Connection))
                {
                    var initializer = new TransactionContextInitializer <TransactionContext>();

                    var recorder = new ConnectionRecorder();
                    DbInterception.Add(recorder);
                    try
                    {
                        using (transactionContext.Database.BeginTransaction())
                        {
                            initializer.InitializeDatabase(transactionContext);
                        }

                        Assert.Equal(1, recorder.Connections.Count);
                    }
                    finally
                    {
                        DbInterception.Remove(recorder);
                    }
                }
            }
        }
Пример #2
0
        public void InitializeDatabase(TContext context)
        {
            EntityConnection connection = (EntityConnection)context.ObjectContext.Connection;

            if (connection.State != ConnectionState.Open)
            {
                return;
            }
            if (connection.CurrentTransaction == null)
            {
                return;
            }
            try
            {
                using (new TransactionScope(TransactionScopeOption.Suppress))
                    context.Transactions.AsNoTracking <TransactionRow>().WithExecutionStrategy <TransactionRow>((IDbExecutionStrategy) new DefaultExecutionStrategy()).Count <TransactionRow>();
            }
            catch (EntityException ex)
            {
                IEnumerable <MigrationStatement> migrationStatements = TransactionContextInitializer <TContext> .GenerateMigrationStatements((TransactionContext)context);

                DbMigrator dbMigrator = new DbMigrator(context.InternalContext.MigrationsConfiguration, (DbContext)context, DatabaseExistenceState.Exists, true);
                using (new TransactionScope(TransactionScopeOption.Suppress))
                    dbMigrator.ExecuteStatements(migrationStatements, connection.CurrentTransaction.StoreTransaction);
            }
        }
Пример #3
0
        /// <inheritdoc />
        public override string BuildDatabaseInitializationScript()
        {
            if (this.TransactionContext == null)
            {
                return((string)null);
            }
            IEnumerable <MigrationStatement> migrationStatements = TransactionContextInitializer <TransactionContext> .GenerateMigrationStatements(this.TransactionContext);

            StringBuilder sqlBuilder = new StringBuilder();

            MigratorScriptingDecorator.BuildSqlScript(migrationStatements, sqlBuilder);
            return(sqlBuilder.ToString());
        }
Пример #4
0
        /// <inheritdoc/>
        public override string BuildDatabaseInitializationScript()
        {
            if (TransactionContext != null)
            {
                var sqlStatements = TransactionContextInitializer <TransactionContext> .GenerateMigrationStatements(TransactionContext);

                var sqlBuilder = new StringBuilder();
                MigratorScriptingDecorator.BuildSqlScript(sqlStatements, sqlBuilder);

                return(sqlBuilder.ToString());
            }

            return(null);
        }