Esempio n. 1
0
        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).");
            }

            _isDisposed  = false;
            _isCompleted = false;
            _isReadOnly  = readOnly;

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

                _isNested = true;
                _manager  = _parentScope._manager;
            }
            else
            {
                _isNested = false;
                _manager  = new DbContextManager(readOnly, isolationLevel, dbContextFactory);
            }

            SetAmbientScope(this);
        }
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

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

            _isDisposed = true;
        }
Esempio n. 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("newAmbientScope");
            }

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

            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);
        }
Esempio n. 4
0
        public TDbContext Get <TDbContext>() where TDbContext : DbContext
        {
            var ambientDbContextScope = DbContextScope.GetAmbientScope();

            return(ambientDbContextScope == null ? null : ambientDbContextScope.Manager.Get <TDbContext>());
        }
Esempio n. 5
0
 public ReadOnlyDbContextScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory);
 }