public DataContextScope(DataContextScopeOption joiningOption, bool readOnly, IsolationLevel?isolationLevel, IDataContextFactory dataContextFactory = null)
        {
            if (isolationLevel.HasValue && joiningOption == DataContextScopeOption.JoinExisting)
            {
                throw new ArgumentException("Cannot join an ambient DataContextScope 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 == DataContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._readOnly)
                {
                    throw new InvalidOperationException("Cannot nest a read/write DataContextScope within a read-only DataContextScope.");
                }

                _nested       = true;
                _dataContexts = _parentScope._dataContexts;
            }
            else
            {
                _nested       = false;
                _dataContexts = new DataContextCollection(readOnly, isolationLevel, dataContextFactory);
            }

            SetAmbientScope(this);
        }
示例#2
0
        public DbContextScope(DataContextScopeOption joiningOption, bool readOnly, IsolationLevel? isolationLevel, IDataContextFactory dbContextFactory = null)
        {
            if (isolationLevel.HasValue && joiningOption == DataContextScopeOption.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 == DataContextScopeOption.JoinExisting)
            {
                if (_parentScope._readOnly && !this._readOnly)
                {
                    throw new InvalidOperationException("Cannot nest a read/write DbContextScope within a read-only DbContextScope.");
                }

                _nested = true;
                _dataContexts = _parentScope._dataContexts;
            }
            else
            {
                _nested = false;
                _dataContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory);
            }

            SetAmbientScope(this);
        }
 public IDataContextReadOnlyScope CreateReadOnly(DataContextScopeOption joiningOption = DataContextScopeOption.JoinExisting)
 {
     return new DataContextReadOnlyScope(
       joiningOption: joiningOption,
       isolationLevel: null,
       dataContextFactory: _dataContextFactory);
 }
 public IDataContextReadOnlyScope CreateReadOnly(DataContextScopeOption joiningOption = DataContextScopeOption.JoinExisting)
 {
     return(new DataContextReadOnlyScope(
                joiningOption: joiningOption,
                isolationLevel: null,
                dataContextFactory: _dataContextFactory));
 }
示例#5
0
 public IDataContextScope Create(DataContextScopeOption joiningOption = DataContextScopeOption.JoinExisting)
 {
     return new DbContextScope(
         joiningOption: joiningOption, 
         readOnly: false, 
         isolationLevel: null, 
         dbContextFactory: _dbContextFactory);
 }
示例#6
0
        /// <summary>
        /// Invoke a <paramref name="func"/> within a <see cref="DataContextScope"/>.
        /// </summary>
        /// <typeparam name="TResult">The return <see cref="Type"/>.</typeparam>
        /// <param name="func">The <see cref="Func{TResult}"/> to invoke.</param>
        /// <param name="option">The <see cref="DataContextScopeOption"/>.</param>
        /// <returns>The result of the function.</returns>
        public static TResult Invoke <TResult>(Func <TResult> func, DataContextScopeOption option = DataContextScopeOption.UseExisting)
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            using (Begin(option))
            {
                return(func.Invoke());
            }
        }
示例#7
0
        /// <summary>
        /// Invoke an <paramref name="action"/> within a <see cref="DataContextScope"/>.
        /// </summary>
        /// <param name="action">The <see cref="Action"/> to invoke.</param>
        /// <param name="option">The <see cref="DataContextScopeOption"/>.</param>
        public static void Invoke(Action action, DataContextScopeOption option = DataContextScopeOption.UseExisting)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using (Begin(option))
            {
                action.Invoke();
            }
        }
示例#8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataContextScope"/> class referencing its parent.
 /// </summary>
 private DataContextScope(DataContextScope parent, DataContextScopeOption option)
 {
     _parent = parent;
     _option = option;
     if (IsPrimaryInstance)
     {
         _dataContexts = new Dictionary <Type, object>();
     }
     else
     {
         _dataContexts = parent._dataContexts;
     }
 }
 public DataContextReadOnlyScope(DataContextScopeOption joiningOption, IsolationLevel? isolationLevel, IDataContextFactory dataContextFactory = null)
 {
     _internalScope = new DataContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dataContextFactory: dataContextFactory);
 }
示例#10
0
 /// <summary>
 /// Begins a new <see cref="DataContextScope"/>.
 /// </summary>
 /// <param name="option">The <see cref="DataContextScopeOption"/>.</param>
 /// <returns>A <see cref="DataContextScope"/>.</returns>
 /// <remarks>The <paramref name="option"/> determines whether a new connection is created or the existing (default) is leveraged.</remarks>
 public static DataContextScope Begin(DataContextScopeOption option = DataContextScopeOption.UseExisting)
 {
     Current = new DataContextScope(Current, option);
     return(Current);
 }
 public DataContextReadOnlyScope(DataContextScopeOption joiningOption, IsolationLevel?isolationLevel, IDataContextFactory dataContextFactory = null)
 {
     _internalScope = new DataContextScope(joiningOption : joiningOption, readOnly : true, isolationLevel : isolationLevel, dataContextFactory : dataContextFactory);
 }