// <summary>
        // Creates a clone of the given <see cref="ObjectContext" />. The underlying <see cref="DbConnection" /> of
        // the context is also cloned and the given connection string is used for the connection string of
        // the cloned connection.
        // </summary>
        public ClonedObjectContext(
            ObjectContextProxy objectContext,
            DbConnection connection,
            string connectionString,
            bool transferLoadedAssemblies = true)
        {
            DebugCheck.NotNull(objectContext);
            // connectionString may be null when connection has been created from DbContextInfo using just a provider

            if (connection == null ||
                connection.State != ConnectionState.Open)
            {
                connection =
                    DbProviderServices.GetProviderFactory(objectContext.Connection.StoreConnection).CreateConnection();
                DbInterception.Dispatch.Connection.SetConnectionString(
                    connection,
                    new DbConnectionPropertyInterceptionContext <string>().WithValue(connectionString));
                _connectionCloned = true;
            }

            _clonedEntityConnection = objectContext.Connection.CreateNew(connection);

            _objectContext = objectContext.CreateNew(_clonedEntityConnection);
            _objectContext.CopyContextOptions(objectContext);

            if (!String.IsNullOrWhiteSpace(objectContext.DefaultContainerName))
            {
                _objectContext.DefaultContainerName = objectContext.DefaultContainerName;
            }

            if (transferLoadedAssemblies)
            {
                TransferLoadedAssemblies(objectContext);
            }
        }
        /// <summary>
        ///     Creates a clone of the given <see cref="ObjectContext" />. The underlying <see cref="DbConnection" /> of
        ///     the context is also cloned and the given connection string is used for the connection string of
        ///     the cloned connection.
        /// </summary>
        public ClonedObjectContext(
            ObjectContextProxy objectContext, string connectionString, bool transferLoadedAssemblies = true)
        {
            DebugCheck.NotNull(objectContext);
            // connectionString may be null when connection has been created from DbContextInfo using just a provider

            var clonedConnection =
                DbProviderServices.GetProviderFactory(objectContext.Connection.StoreConnection).CreateConnection();

            clonedConnection.ConnectionString = connectionString;

            var clonedEntityConnection = objectContext.Connection.CreateNew(clonedConnection);

            _objectContext = objectContext.CreateNew(clonedEntityConnection);
            _objectContext.CopyContextOptions(objectContext);

            if (!String.IsNullOrWhiteSpace(objectContext.DefaultContainerName))
            {
                _objectContext.DefaultContainerName = objectContext.DefaultContainerName;
            }

            if (transferLoadedAssemblies)
            {
                TransferLoadedAssemblies(objectContext);
            }
        }