@@ -224,7 +224,7 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
224224 }
225225
226226 /// <summary>
227- /// Temporarily disables all save changes operations (both explicit ant automatic)
227+ /// Temporarily disables all save changes operations (both explicit ant automatic)
228228 /// for specified <paramref name="target"/>.
229229 /// Such entity is prevented from being persisted to the database,
230230 /// when <see cref="SaveChanges"/> is called or query is executed.
@@ -234,16 +234,20 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
234234 /// all entities that reference <paramref name="target"/> are also pinned automatically.
235235 /// </summary>
236236 /// <param name="target">The entity to disable persisting.</param>
237- /// <returns>A special object that controls lifetime of such behavior if <paramref name="target"/> was not previously processed by the method,
238- /// otherwise <see langword="null"/>.</returns>
237+ /// <returns>
238+ /// A special object that controls lifetime of such behavior if <paramref name="target"/> was not previously processed by the method
239+ /// and automatic saving of changes is enabled (<see cref="SessionOptions.AutoSaveChanges"/>),
240+ /// otherwise <see langword="null"/>.
241+ /// </returns>
239242 public IDisposable DisableSaveChanges ( IEntity target )
240243 {
241244 EnsureNotDisposed ( ) ;
242245 ArgumentValidator . EnsureArgumentNotNull ( target , "target" ) ;
246+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) )
247+ return null ; // No need to pin in this case
248+
243249 var targetEntity = ( Entity ) target ;
244250 targetEntity . EnsureNotRemoved ( ) ;
245- if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) )
246- return new Disposable ( b => { return ; } ) ; // No need to pin in this case
247251 return pinner . RegisterRoot ( targetEntity . State ) ;
248252 }
249253
@@ -252,15 +256,15 @@ public IDisposable DisableSaveChanges(IEntity target)
252256 /// Explicit call of <see cref="SaveChanges"/> will lead to flush changes anyway.
253257 /// If save changes is to be performed due to starting a nested transaction or committing a transaction,
254258 /// active disabling save changes scope will lead to failure.
255- /// <returns>A special object that controls lifetime of such behavior if there is no active scope,
259+ /// <returns>A special object that controls lifetime of such behavior if there is no active scope
260+ /// and automatic saving of changes is enabled (<see cref="SessionOptions.AutoSaveChanges"/>),
256261 /// otherwise <see langword="null"/>.</returns>
257262 /// </summary>
258263 public IDisposable DisableSaveChanges ( )
259264 {
260- if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) )
261- return new Disposable ( b => { return ; } ) ; // No need to pin in this case
262- if ( disableAutoSaveChanges )
263- return null ;
265+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) || disableAutoSaveChanges ) {
266+ return null ; // No need to pin in these cases
267+ }
264268
265269 disableAutoSaveChanges = true ;
266270 return new Disposable ( _ => {
@@ -317,7 +321,7 @@ private void CancelEntitiesChanges()
317321 newEntity . Update ( null ) ;
318322 newEntity . PersistenceState = PersistenceState . Removed ;
319323 }
320-
324+
321325 foreach ( var modifiedEntity in EntityChangeRegistry . GetItems ( PersistenceState . Modified ) ) {
322326 modifiedEntity . RollbackDifference ( ) ;
323327 modifiedEntity . PersistenceState = PersistenceState . Synchronized ;
0 commit comments