コード例 #1
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return new DbContextReadOnlyScope(
         joiningOption: joiningOption, 
         isolationLevel: null, 
         dbContextFactory: _dbContextFactory);
 }
コード例 #2
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
 {
     return new DbContextReadOnlyScope(
         joiningOption: joiningOption,
         isolationLevel: isolationLevel,
         dbContextFactory: _dbContextFactory);
 }
コード例 #3
0
ファイル: DbContextScope.cs プロジェクト: Misakai/storage
        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);
        }
コード例 #4
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextReadOnlyScope(
                joiningOption: joiningOption,
                isolationLevel: null,
                dbContextFactory: _dbContextFactory,
                defaultDbContextType: _defaultDbContextType));
 }
コード例 #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)
 {
 }
コード例 #6
0
 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);
 }
コード例 #8
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null)
 {
     return(new DbContextScope(
                joiningOption: joiningOption,
                readOnly: false,
                isolationLevel: null,
                dbContextFactory: _dbContextFactory,
                nameOrConnectionString));
 }
コード例 #9
0
        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);
        }
コード例 #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);
    }
コード例 #11
0
ファイル: DbContextScope.cs プロジェクト: Hduc/Project
        /// <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);
        }
コード例 #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();
        }
コード例 #13
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(joiningOption, false, null, _dbContextFactory));
 }
コード例 #14
0
        public IWynnDbContextScope Create(string transactionUser, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            var dbContextScope = _dbContextScopeFactory.Create(joiningOption);

            return(new WynnDbContextScope(dbContextScope, _dbContextLocator));
        }
コード例 #15
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(
                joiningOption: joiningOption,
                isolationLevel: null));
 }
コード例 #16
0
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory);
 }
コード例 #17
0
ファイル: DbScope.cs プロジェクト: hejiangyuan/EF_Sample
 public static IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string domainCode = null)
 {
     return(GetDbContextScopeFactory(domainCode).CreateReadOnly(joiningOption));
 }
コード例 #18
0
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
 {
     _internalScope = new DbContextScope(joiningOption, true, isolationLevel, ambientDbContextFactory, loggerFactory, scopeDiagnostic);
 }
コード例 #19
0
 public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDbContextFactory dbContextFactory = null)
 {
     _internalScope = new DbContextScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dbContextFactory : dbContextFactory);
 }
コード例 #20
0
        public IDbContextReadOnlyScope GetReadOnlyContextScope(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
        {
            var dbContextScopeFactory = new DbContextScopeFactory(site: _site);

            return(dbContextScopeFactory.CreateReadOnly(joiningOption));
        }
コード例 #21
0
 public DbContextScope(DbContextScopeOption joiningOption, IsolationLevel?isolationLevel, IAmbientDbContextFactory ambientDbContextFactory, ILoggerFactory loggerFactory, IScopeDiagnostic scopeDiagnostic)
     : base(joiningOption, false, isolationLevel, ambientDbContextFactory, loggerFactory, scopeDiagnostic)
 {
     _scopeDiagnostic = scopeDiagnostic;
 }
コード例 #22
0
 public IDisposable CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(_logger, joiningOption, true, IsolationLevel.Unspecified, _dbContextFactory));
 }
コード例 #23
0
 public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextScope(_logger, joiningOption, false, IsolationLevel.Unspecified, _dbContextFactory));
 }
コード例 #24
0
 public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel) : this(joiningOption, readOnly, isolationLevel, null)
 {
 }
コード例 #25
0
 public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(new DbContextReadOnlyScope(joiningOption, null, _dbContextFactory));
 }
コード例 #26
0
        /// <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>());
        }
コード例 #27
0
ファイル: DbScope.cs プロジェクト: hejiangyuan/EF_Sample
 public static IDbContextScope Create(DbConnection conn, DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
 {
     return(GetDbContextScopeFactory(conn).Create(joiningOption));
 }