System.Data.Entity.Internal.LazyInternalContext.DisposeContext C# (CSharp) Method

DisposeContext() public method

Disposes the context. The underlying ObjectContext is also disposed. The connection to the database (DbConnection object) is also disposed if it was created by the context, otherwise it is not disposed.
public DisposeContext ( bool disposing ) : void
disposing bool
return void
        public override void DisposeContext(bool disposing)
        {
            if (!IsDisposed)
            {
                base.DisposeContext(disposing);

                if (disposing)
                {
                    // Note: issue 1805 - it is important that the ObjectContext be disposed before the InternalConnection.
                    // If the ObjectContext is responsible for calling Dispose() on the EntityConnection (which is
                    // the case for non-EF connection strings) then the EntityConnection needs the chance to unsubscribe
                    // itself from the underlying connection's StateChange event _before_ that underlying connection is disposed.
                    if (_objectContext != null)
                    {
                        _objectContext.Dispose();
                    }
                    _internalConnection.Dispose();
                }
            }
        }

Usage Example

        public void Dispose_calls_EntityConnection_dispose_before_InternalConnection_dispose()
        {
            var results = new List <string>();

            var underlyingConnection = new Mock <DbConnection>();

            var lazyInternalConnectionMock = new Mock <LazyInternalConnection>("fake");

            lazyInternalConnectionMock.SetupGet(ic => ic.Connection).Returns(underlyingConnection.Object);
            lazyInternalConnectionMock.Setup(ic => ic.Dispose()).Callback(() => results.Add("LazyInternalConnection Dispose() called"));

            var metadataWorkspaceMock = new Mock <MetadataWorkspace>();

            metadataWorkspaceMock.Setup(mw => mw.GetItemCollection(DataSpace.CSSpace)).Returns(() => null);

            var dbContext = new TestDbContext();

            var entityConnectionMock = new Mock <EntityConnection>();

            entityConnectionMock.SetupGet(ec => ec.ConnectionString).Returns("fake");
            entityConnectionMock.Setup(ec => ec.GetMetadataWorkspace()).Returns(metadataWorkspaceMock.Object);
            entityConnectionMock.Protected().Setup("Dispose", ItExpr.IsAny <bool>()).
            Callback <bool>(b => results.Add("EntityConnection Dispose() called"));

            var objectContextMock = new Mock <ObjectContext>(entityConnectionMock.Object, true)
            {
                CallBase = true
            };

            objectContextMock.SetupGet(oc => oc.MetadataWorkspace).Returns(metadataWorkspaceMock.Object);

            var lazyInternalContext = new LazyInternalContext(
                dbContext, lazyInternalConnectionMock.Object, null, null, null, null, objectContextMock.Object);

            lazyInternalContext.DisposeContext(true);

            Assert.Equal(2, results.Count);
            Assert.Same("EntityConnection Dispose() called", results[0]);
            Assert.Same("LazyInternalConnection Dispose() called", results[1]);
        }
All Usage Examples Of System.Data.Entity.Internal.LazyInternalContext::DisposeContext