コード例 #1
0
        public new void Dispose()
        {
            base.Dispose();
            var memento = _ambientDbContextHouseKeeper.Memento;

            CallContextContextData.AddContextData(ContextKey, memento.ContextData);
        }
コード例 #2
0
        internal virtual void Initialize()
        {
            _savedContextData = null;
            var contextData = CallContextContextData.GetContextData();

            if (contextData == null)
            {
                Debug.WriteLine("Creating a new DbContext");
                contextData = ContextData.CreateContextData <T>(_mode, _isolationLevel, _dbTransaction,
                                                                _sqlConnection);
                _isParentScope = true;
            }
            else
            {
                _isParentScope = false;
                var dbcontext = contextData.GetDbContextByType <T>();
                if (dbcontext == null)
                {
                    Debug.WriteLine("Creating a new DbContext");
                    contextData.CreateNewDbContextIfNotExists <T>(_mode, _isolationLevel, _dbTransaction,
                                                                  _sqlConnection);
                }
                else
                {
                    Debug.WriteLine("DbContext exists in CallContext, just re-using it");
                    if (_isolationLevel != null && _isolationLevel != IsolationLevel.ReadCommitted && contextData.IsolationLevel != _isolationLevel)
                    {
                        throw new InvalidOperationException("Cannot change the isolation level on an Ambient transaction");
                    }

                    if (dbcontext.Mode == DbContextOption.Mode.Read && _mode == DbContextOption.Mode.Write)
                    {
                        throw new InvalidOperationException("Cannot nested read/write when parent is in read mode");
                    }
                }
            }

            _savedContextData = contextData;

            //Check the callcontext to see if there is an existing context data.
            contextData = CallContextContextData.GetContextData();
            //Add the context data back to the CallContext with new context key
            if (contextData == null)
            {
                CallContextContextData.AddContextData(ContextKey, _savedContextData);
            }
        }