public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return new DbContextReadOnlyScope(
         joiningOption: joiningOption, 
         isolationLevel: null, 
         dbContextFactory: _dbContextFactory);
 }
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
 {
     return new DbContextReadOnlyScope(
         joiningOption: joiningOption,
         isolationLevel: isolationLevel,
         dbContextFactory: _dbContextFactory);
 }
Exemplo n.º 3
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).");

            _disposed = false;
            _completed = false;
            _readOnly = readOnly;

            _parentScope = GetAmbientScope();
            if (_parentScope != null && joiningOption == DbContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._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);
        }
Exemplo n.º 4
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextReadOnlyScope(
                joiningOption: joiningOption,
                isolationLevel: null,
                dbContextFactory: _dbContextFactory,
                defaultDbContextType: _defaultDbContextType));
 }
Exemplo n.º 5
0
 // This was forcing JoinExisting option instead of taking the passed in option
 // It is being handled with an override constructor in DbContextReadOnlyScope for ForceCreateNew
 public DbContextScope(DbContextScopeOption joiningOption,
                       bool readOnly,
                       // ReSharper disable once UnusedParameter.Local
                       IsolationLevel?isolationLevel,
                       IDbContextFactory dbContextFactory = null)
     : this(joiningOption, readOnly, null, true, dbContextFactory)
 {
 }
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(
                joiningOption: joiningOption,
                readOnly: false,
                isolationLevel: null,
                dbContextFactory: _dbContextFactory));
 }
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return new DbContextScope(
         joiningOption: joiningOption, 
         readOnly: false, 
         isolationLevel: null, 
         dbContextFactory: this.dbContextFactory);
 }
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null)
 {
     return(new DbContextScope(
                joiningOption: joiningOption,
                readOnly: false,
                isolationLevel: null,
                dbContextFactory: _dbContextFactory,
                nameOrConnectionString));
 }
        public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            checkDisposed();

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

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

            return(scope);
        }
Exemplo n.º 10
0
    public DbContextScope(ILogger <DbContextScope> logger, DbContextScopeOption joiningOption, bool readOnly = false,
                          IsolationLevel isolationLevel      = IsolationLevel.Unspecified,
                          IDbContextFactory?dbContextFactory = null)
    {
        if (isolationLevel != IsolationLevel.Unspecified)
        {
            joiningOption = DbContextScopeOption.CreateNew;
        }

        _logger        = logger;
        _joiningOption = joiningOption;
        _readOnly      = readOnly;

        _parentScope = GetAmbientScope();

        if (joiningOption == DbContextScopeOption.Suppress)
        {
#if DEBUG
            _logger.LogDebug("Start suppressing an ambient DbContext scope");
#endif
            ambientDbContextScopeIdHolder.Value = null;
            return;
        }

        if (_parentScope != null && joiningOption == DbContextScopeOption.JoinExisting &&
            (!_parentScope._readOnly || _readOnly))
        {
#if DEBUG
            _logger.LogDebug("Join existing DbContext scope");
#endif
            _nested    = true;
            DbContexts = _parentScope.DbContexts;
        }
        else
        {
#if DEBUG
            _logger.LogDebug("Start new DbContext scope");
#endif
            DbContexts = new(_readOnly, isolationLevel, dbContextFactory);
        }

        SetAmbientScope(this);
    }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        protected DbContextScopeBase(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
        {
            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).");
            }

            _scopeDiagnostic = scopeDiagnostic;
            _logger          = loggerFactory.Create <DbContextScopeBase>();

            _readonly = readOnly;

            _disposed  = false;
            _completed = false;

            _parentScope = AmbientContextScopeMagic.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(this, ambientDbContextFactory, loggerFactory, readOnly, isolationLevel);
            }

            SetScope();
        }
Exemplo n.º 13
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(joiningOption, false, null, _dbContextFactory));
 }
Exemplo n.º 14
0
        public IWynnDbContextScope Create(string transactionUser, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            var dbContextScope = _dbContextScopeFactory.Create(joiningOption);

            return(new WynnDbContextScope(dbContextScope, _dbContextLocator));
        }
Exemplo n.º 15
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(
                joiningOption: joiningOption,
                isolationLevel: null));
 }
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory);
 }
Exemplo n.º 17
0
 public static IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string domainCode = null)
 {
     return(GetDbContextScopeFactory(domainCode).CreateReadOnly(joiningOption));
 }
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
 {
     _internalScope = new DbContextScope(joiningOption, true, isolationLevel, ambientDbContextFactory, loggerFactory, scopeDiagnostic);
 }
Exemplo n.º 19
0
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dbContextFactory : dbContextFactory);
 }
Exemplo n.º 20
0
        public IDbContextReadOnlyScope GetReadOnlyContextScope(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            var dbContextScopeFactory = new DbContextScopeFactory(site: _site);

            return(dbContextScopeFactory.CreateReadOnly(joiningOption));
        }
Exemplo n.º 21
0
 public DbContextScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
     : base(joiningOption, false, isolationLevel, ambientDbContextFactory, loggerFactory, scopeDiagnostic)
 {
     _scopeDiagnostic = scopeDiagnostic;
 }
 public IDisposable CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(_logger, joiningOption, true, IsolationLevel.Unspecified, _dbContextFactory));
 }
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(_logger, joiningOption, false, IsolationLevel.Unspecified, _dbContextFactory));
 }
Exemplo n.º 24
0
 public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel) : this(joiningOption, readOnly, isolationLevel, null)
 {
 }
Exemplo n.º 25
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextReadOnlyScope(joiningOption, null, _dbContextFactory));
 }
        /// <summary>
        /// Creates a new DbContextScope for read-only queries.
        /// By default, the new scope will join the existing ambient scope. This
        /// is what you want in most cases. This ensures that the same DbContext instances
        /// are used by all services methods called within the scope of a business transaction.
        /// Set 'joiningOption' to 'ForceCreateNew' if you want to ignore the ambient scope
        /// and force the creation of new DbContext instances within that scope. Using 'ForceCreateNew'
        /// is an advanced feature that should be used with great care and only if you fully understand the
        /// implications of doing this.
        /// </summary>
        public static TDbContext OpenReadOnly <TDbContext>(this IDbContextScopeFactory self, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting) where TDbContext : DbContext
        {
            var dbContextScope = self.CreateReadOnly(joiningOption);

            return(dbContextScope.Get <TDbContext>());
        }
Exemplo n.º 27
0
 public static IDbContextScope Create(DbConnection conn, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(GetDbContextScopeFactory(conn).Create(joiningOption));
 }