/// <summary> /// Parameterized constructor. /// </summary> /// <param name="saveAllChangesAtEndOfScope"> /// A boolean value that indicates whether to automatically save /// all object changes at end of the scope. /// </param> public ObjectContextScope(bool saveAllChangesAtEndOfScope) { if (_currentScope != null && !_currentScope._isDisposed) { throw new InvalidOperationException("ObjectContextScope instances cannot be nested."); } _saveAllChangesAtEndOfScope = saveAllChangesAtEndOfScope; _objectContext = new T(); _isDisposed = false; Thread.BeginThreadAffinity(); System.Diagnostics.Debug.WriteLine("Begin of ObjectContextScope"); _currentScope = this; }
/// <summary> /// Disposes the ObjectContext. /// </summary> public void Dispose() { if (!_isDisposed) { _currentScope = null; Thread.EndThreadAffinity(); if (_saveAllChangesAtEndOfScope) { _objectContext.SaveChanges(); } _objectContext.Dispose(); _isDisposed = true; System.Diagnostics.Debug.WriteLine("End of ObjectContextScope"); } }