public IPersistenceUnitOfWork Create(
     PersistenceUnitOfWorkOption joiningOption = PersistenceUnitOfWorkOption.JoinExisting,
     bool readOnly = false)
 {
     return(new PersistenceScopeImpl(
                _persistenceContextFactory,
                joiningOption,
                readOnly: readOnly,
                isolationLevel: null));
 }
            public PersistenceScopeImpl(
                IPersistenceContextFactory persistenceContextFactory,
                PersistenceUnitOfWorkOption joiningOption,
                bool readOnly,
                IsolationLevel?isolationLevel)
            {
                _persistenceContextFactory = persistenceContextFactory;
                if (isolationLevel.HasValue &&
                    joiningOption == PersistenceUnitOfWorkOption.JoinExisting)
                {
                    throw new ArgumentException(
                              @"Cannot join an ambient persistence scope 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 scope (i.e. the 'joinAmbient' parameter must be set to false).");
                }

                _disposed    = false;
                _completed   = false;
                _readOnly    = readOnly;
                _parentScope = GetAmbientScope();

                if (_parentScope != null && joiningOption == PersistenceUnitOfWorkOption.JoinExisting)
                {
                    if (_parentScope._readOnly && !this._readOnly)
                    {
                        throw new InvalidOperationException("Cannot nest a read/write Scope within a read-only Scope.");
                    }
                    _nested             = true;
                    _persistenceContext = _parentScope._persistenceContext;

                    if (_persistenceContext as IPersistenceContextExplicit == null)
                    {
                        throw new InvalidProgramException($"Parent PersistenceContext does not implement {typeof(IPersistenceContextExplicit).Name}");
                    }
                }
                else
                {
                    _nested             = false;
                    _persistenceContext = _persistenceContextFactory.Create();

                    var explicitContext = _persistenceContext as IPersistenceContextExplicit;
                    if (explicitContext == null)
                    {
                        throw new InvalidProgramException($"PersistenceContext does not implement {typeof(IPersistenceContextExplicit).Name}");
                    }

                    if (isolationLevel.HasValue)
                    {
                        _persistenceTransaction = explicitContext.BeginTransaction(isolationLevel.Value);
                    }
                }
                SetAmbientScope(this);
            }