Example #1
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            if (_savedScope != null)
            {
                DbContextScope.SetAmbientScope(_savedScope);

                _savedScope = null;
            }

            _disposed = true;
        }
Example #2
0
        /// <summary>
        /// </summary>
        /// <param name="joiningOption"></param>
        /// <param name="readOnly"></param>
        /// <param name="isolationLevel"></param>
        /// <param name="dbContextFactory"></param>
        public DbContextScope(DbContextScopeOption joiningOption
                              , bool readOnly
                              , IsolationLevel?isolationLevel
                              , IDbContextFactory dbContextFactory = null)
        {
            if (isolationLevel.HasValue && joiningOption == DbContextScopeOption.JoinExisting)
            {
                throw new ArgumentException(@"
                    Cannot join an ambient DbContextScope when an explicit database transaction is required. 
                    When requiring explicit database transactions to be used 
                    (i.e. when the 'isolationLevel' parameter is set), 
                    you must not also ask to join the ambient context 
                    (i.e. the 'joinAmbient' parameter must be set to false).");
            }

            _disposed  = false;
            _completed = false;

            _readOnly = readOnly;

            _parentScope = GetAmbientScope();

            if (_parentScope != null && joiningOption == DbContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !_readOnly)
                {
                    throw new InvalidOperationException(@"
                        Cannot nest a read/write DbContextScope within a read-only DbContextScope.");
                }

                _nested = true;

                _dbContexts = _parentScope._dbContexts;
            }
            else
            {
                _nested = false;

                _dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory);
            }

            SetAmbientScope(this);
        }
Example #3
0
        /// <summary>
        ///     Makes the provided 'dbContextScope' available as the the ambient scope via the CallContext.
        /// </summary>
        internal static void SetAmbientScope(DbContextScope newAmbientScope)
        {
            if (newAmbientScope == null)
            {
                throw new ArgumentNullException(nameof(newAmbientScope));
            }

            var current = CallContext.LogicalGetData(AmbientDbContextScopeKey) as InstanceIdentifier;

            if (current == newAmbientScope._instanceIdentifier)
            {
                return;
            }

            // Store the new scope's instance identifier in the CallContext, making it the ambient scope
            CallContext.LogicalSetData(AmbientDbContextScopeKey, newAmbientScope._instanceIdentifier);

            // Keep track of this instance (or do nothing if we're already tracking it)
            DbContextScopeInstances.GetValue(newAmbientScope._instanceIdentifier, key => newAmbientScope);
        }
Example #4
0
 /// <summary>
 /// </summary>
 /// <param name="joiningOption"></param>
 /// <param name="isolationLevel"></param>
 /// <param name="dbContextFactory"></param>
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption
                               , IsolationLevel?isolationLevel
                               , IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption, true, isolationLevel, dbContextFactory);
 }
Example #5
0
 public TDbContext Get <TDbContext>() where TDbContext : DbContext
 {
     return(DbContextScope.GetAmbientScope()?.DbContexts.Get <TDbContext>());
 }