Пример #1
0
        public void DbContext_can_be_initialized_without_promotion_to_distributed_transaction_inside_user_transaction_if_the_context_type_has_been_previously_initalized_outside()
        {
            var connectionString = default(string);
            using (var context = new TransactionTestsContext())
            {
                // force context initialization
                ExtendedSqlAzureExecutionStrategy.ExecuteNew(() => context.Entities.Count());
                connectionString = context.Database.Connection.ConnectionString;
            }

            // this only works for integrated security, or when password is persisted after connecting
            // otherwise we can't connect to database during context initialization (password is gone from connection string)
            if (DatabaseTestHelpers.IsIntegratedSecutity(connectionString) ||
                DatabaseTestHelpers.PersistsSecurityInfo(connectionString))
            {
                using (var connection = new SqlConnection(connectionString))
                {
                    ExtendedSqlAzureExecutionStrategy.ExecuteNew(
                        () =>
                        {
                            connection.Open();
                            using (var transaction = connection.BeginTransaction())
                            {
                                using (var context = new TransactionTestsContext(connection, false))
                                {
                                    context.Database.UseTransaction(transaction);
                                    context.Entities.Count();
                                }
                            }
                        });
                }
            }
        }
        public void DbContext_can_be_initialized_without_promotion_to_distributed_transaction_inside_user_transaction_if_the_context_type_has_been_previously_initalized_outside()
        {
            var connectionString = default(string);
            using (var context = new TransactionTestsContext())
            {
                // force context initialization
                context.Entities.Count();
                connectionString = context.Database.Connection.ConnectionString;
            }

            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    using (var context = new TransactionTestsContext(connection, false))
                    {
                        context.Database.UseTransaction(transaction);
                        context.Entities.Count();
                    }
                }
            }
        }