Ejemplo n.º 1
0
 public ClonedObjectContext(
     ObjectContextProxy objectContext,
     DbConnection connection,
     string connectionString,
     bool transferLoadedAssemblies = true)
 {
     if (connection == null || connection.State != ConnectionState.Open)
     {
         connection = DbProviderServices.GetProviderFactory(objectContext.Connection.StoreConnection).CreateConnection();
         DbInterception.Dispatch.Connection.SetConnectionString(connection, new DbConnectionPropertyInterceptionContext <string>().WithValue(connectionString));
         this._connectionCloned = true;
     }
     this._clonedEntityConnection = objectContext.Connection.CreateNew(connection);
     this._objectContext          = objectContext.CreateNew(this._clonedEntityConnection);
     this._objectContext.CopyContextOptions(objectContext);
     if (!string.IsNullOrWhiteSpace(objectContext.DefaultContainerName))
     {
         this._objectContext.DefaultContainerName = objectContext.DefaultContainerName;
     }
     if (!transferLoadedAssemblies)
     {
         return;
     }
     this.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,
            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);
            }
        }
Ejemplo n.º 3
0
        /// <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);
            }
        }
Ejemplo n.º 4
0
        // <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();
            }
        }
Ejemplo n.º 5
0
        private void TransferLoadedAssemblies(ObjectContextProxy source)
        {
            IEnumerable <GlobalItem> objectItemCollection = source.GetObjectItemCollection();

            foreach (Assembly assembly in objectItemCollection.Where <GlobalItem>((Func <GlobalItem, bool>)(i =>
            {
                if (!(i is EntityType))
                {
                    return(i is ComplexType);
                }
                return(true);
            })).Select <GlobalItem, Assembly>((Func <GlobalItem, Assembly>)(i => source.GetClrType((StructuralType)i).Assembly())).Union <Assembly>(objectItemCollection.OfType <EnumType>().Select <EnumType, Assembly>((Func <EnumType, Assembly>)(i => source.GetClrType(i).Assembly()))).Distinct <Assembly>())
            {
                this._objectContext.LoadFromAssembly(assembly);
            }
        }
Ejemplo n.º 6
0
        public void Dispose()
        {
            if (this._objectContext == null)
            {
                return;
            }
            ObjectContextProxy objectContext = this._objectContext;
            DbConnection       connection    = this.Connection;

            this._objectContext = (ObjectContextProxy)null;
            objectContext.Dispose();
            this._clonedEntityConnection.Dispose();
            if (!this._connectionCloned)
            {
                return;
            }
            DbInterception.Dispatch.Connection.Dispose(connection, new DbInterceptionContext());
        }
        // <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)
        {
            DebugCheck.NotNull(source);

            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>
        // 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());
                }
            }
        }