public DbContextCollection(DbContextScope dbContextScope, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, bool readOnly, IsolationLevel?isolationLevel)
        {
            _disposed  = false;
            _completed = false;
            _logger    = loggerFactory.CreateLogger <DbContextCollection>();

            InitializedDbContexts = new Dictionary <Type, (DbContext DbContext, IDbContextProxyBypass Proxy)>();
            _transactions         = new Dictionary <DbContext, IDbContextTransaction>();

            _dbContextScope          = dbContextScope;
            _readOnly                = readOnly;
            _isolationLevel          = isolationLevel;
            _ambientDbContextFactory = ambientDbContextFactory;
        }
Example #2
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

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

            _disposed = true;
        }
Example #3
0
        public IDbContextScope CreateWithTransaction(IsolationLevel isolationLevel)
        {
            checkDisposed();

            var scope = new DbContextScope(
                DbContextScopeOption.ForceCreateNew,
                isolationLevel,
                _ambientDbContextFactory,
                _loggerFactory,
                _scopeDiagnostic);

            _disposables.Add(new WeakReference <IDisposable>(scope));

            return(scope);
        }
Example #4
0
        public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            checkDisposed();

            var scope = new DbContextScope(
                joiningOption,
                null,
                _ambientDbContextFactory,
                _loggerFactory,
                _scopeDiagnostic);

            _disposables.Add(new WeakReference <IDisposable>(scope));

            return(scope);
        }
Example #5
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.GetData(AmbientDbContextScopeKey) as InstanceIdentifier;

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

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

            // Keep track of this instance (or do nothing if we're already tracking it)
            DbContextScopeInstances.GetValue(newAmbientScope._instanceIdentifier, key => newAmbientScope);
        }
        /// <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));
            }

            //return Thread.CurrentThread.GetExecutionContextReader().LogicalCallContext.GetData(name);
            var current = CallContext.GetData <InstanceIdentifier>(ambientDbContextScopeKey);

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

            // Store the new scope's instance identifier in the CallContext, making it the ambient scope
            CallContext.SetData(ambientDbContextScopeKey, newAmbientScope.InstanceIdentifier);

            // Keep track of this instance (or do nothing if we're already tracking it)
            dbContextScopeInstances.GetValue(newAmbientScope.InstanceIdentifier, key => newAmbientScope);
        }
Example #7
0
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dbContextFactory : dbContextFactory);
 }
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
 {
     _internalScope = new DbContextScope(joiningOption, true, isolationLevel, ambientDbContextFactory, loggerFactory, scopeDiagnostic);
 }