// <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);
            }
        }
Exemple #3
0
 public virtual void CopyContextOptions(ObjectContextProxy source)
 {
     this._objectContext.ContextOptions.LazyLoadingEnabled                 = source._objectContext.ContextOptions.LazyLoadingEnabled;
     this._objectContext.ContextOptions.ProxyCreationEnabled               = source._objectContext.ContextOptions.ProxyCreationEnabled;
     this._objectContext.ContextOptions.UseCSharpNullComparisonBehavior    = source._objectContext.ContextOptions.UseCSharpNullComparisonBehavior;
     this._objectContext.ContextOptions.UseConsistentNullReferenceBehavior = source._objectContext.ContextOptions.UseConsistentNullReferenceBehavior;
     this._objectContext.ContextOptions.UseLegacyPreserveChangesBehavior   = source._objectContext.ContextOptions.UseLegacyPreserveChangesBehavior;
     this._objectContext.CommandTimeout      = source._objectContext.CommandTimeout;
     this._objectContext.InterceptionContext = source._objectContext.InterceptionContext.WithObjectContext(this._objectContext);
 }
Exemple #4
0
 public virtual void CopyContextOptions(ObjectContextProxy source)
 {
     _objectContext.ContextOptions.LazyLoadingEnabled              = source._objectContext.ContextOptions.LazyLoadingEnabled;
     _objectContext.ContextOptions.ProxyCreationEnabled            = source._objectContext.ContextOptions.ProxyCreationEnabled;
     _objectContext.ContextOptions.UseCSharpNullComparisonBehavior =
         source._objectContext.ContextOptions.UseCSharpNullComparisonBehavior;
     _objectContext.ContextOptions.UseConsistentNullReferenceBehavior =
         source._objectContext.ContextOptions.UseConsistentNullReferenceBehavior;
     _objectContext.ContextOptions.UseLegacyPreserveChangesBehavior =
         source._objectContext.ContextOptions.UseLegacyPreserveChangesBehavior;
 }
 public virtual void CopyContextOptions(ObjectContextProxy source)
 {
     _objectContext.ContextOptions.LazyLoadingEnabled = source._objectContext.ContextOptions.LazyLoadingEnabled;
     _objectContext.ContextOptions.ProxyCreationEnabled = source._objectContext.ContextOptions.ProxyCreationEnabled;
     _objectContext.ContextOptions.UseCSharpNullComparisonBehavior =
         source._objectContext.ContextOptions.UseCSharpNullComparisonBehavior;
     _objectContext.ContextOptions.UseConsistentNullReferenceBehavior =
         source._objectContext.ContextOptions.UseConsistentNullReferenceBehavior;
     _objectContext.ContextOptions.UseLegacyPreserveChangesBehavior =
         source._objectContext.ContextOptions.UseLegacyPreserveChangesBehavior;
 }
Exemple #6
0
        public virtual void CopyContextOptions(ObjectContextProxy source)
        {
            _objectContext.ContextOptions.LazyLoadingEnabled              = source._objectContext.ContextOptions.LazyLoadingEnabled;
            _objectContext.ContextOptions.ProxyCreationEnabled            = source._objectContext.ContextOptions.ProxyCreationEnabled;
            _objectContext.ContextOptions.UseCSharpNullComparisonBehavior =
                source._objectContext.ContextOptions.UseCSharpNullComparisonBehavior;
            _objectContext.ContextOptions.UseConsistentNullReferenceBehavior =
                source._objectContext.ContextOptions.UseConsistentNullReferenceBehavior;
            _objectContext.ContextOptions.UseLegacyPreserveChangesBehavior =
                source._objectContext.ContextOptions.UseLegacyPreserveChangesBehavior;
            _objectContext.CommandTimeout = source._objectContext.CommandTimeout;
            _objectContext.ContextOptions.DisableFilterOverProjectionSimplificationForCustomFunctions = source._objectContext.ContextOptions.DisableFilterOverProjectionSimplificationForCustomFunctions;

            _objectContext.InterceptionContext = source._objectContext.InterceptionContext.WithObjectContext(_objectContext);
        }
        /// <summary>
        ///     Finds the assemblies that were used for loading o-space types in the source context
        ///     and loads those assemblies in the cloned context.
        /// </summary>
        private void TransferLoadedAssemblies(ObjectContextProxy source)
        {
            Contract.Requires(source != null);

            var objectItemCollection = source.GetObjectItemCollection();

            var assemblies = objectItemCollection
                .Where(i => i is EntityType || i is ComplexType)
                .Select(i => source.GetClrType((StructuralType)i).Assembly)
                .Union(
                    objectItemCollection.OfType<EnumType>()
                        .Select(i => source.GetClrType(i).Assembly))
                .Distinct();

            foreach (var assembly in assemblies)
            {
                _objectContext.LoadFromAssembly(assembly);
            }
        }
        /// <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)
        {
            Contract.Requires(objectContext != null);
            Contract.Requires(!string.IsNullOrWhiteSpace(connectionString));

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

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

            _objectContext = objectContext.CreateNew(clonedEntityConnection);

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

            if (transferLoadedAssemblies)
            {
                TransferLoadedAssemblies(objectContext);
            }
        }
        /// <summary>
        /// Disposes both the underlying ObjectContext and its store connection.
        /// </summary>
        public void Dispose()
        {
            if (_objectContext != null)
            {
                var tempContext = _objectContext;
                var connection = Connection;

                _objectContext = null;

                tempContext.Dispose();
                connection.Dispose();
            }
        }
        // <summary>
        // Disposes both the underlying ObjectContext and its store connection.
        // </summary>
        public void Dispose()
        {
            if (_objectContext != null)
            {
                var tempContext = _objectContext;
                var connection = Connection;

                _objectContext = null;

                tempContext.Dispose();

                // EntityConnection should be disposed of before store connection is disposed. EntityConnection dispose method unsubscribes from StateChanged event 
                // on the underlying store connection, so if order is reversed we try to modify an already disposed object.
                _clonedEntityConnection.Dispose();

                if (_connectionCloned)
                {
                    DbInterception.Dispatch.Connection.Dispose(connection, new DbInterceptionContext());
                }
            }
        }