@@ -224,7 +224,7 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
224
224
}
225
225
226
226
/// <summary>
227
- /// Temporarily disables all save changes operations (both explicit ant automatic)
227
+ /// Temporarily disables all save changes operations (both explicit ant automatic)
228
228
/// for specified <paramref name="target"/>.
229
229
/// Such entity is prevented from being persisted to the database,
230
230
/// when <see cref="SaveChanges"/> is called or query is executed.
@@ -234,34 +234,37 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
234
234
/// all entities that reference <paramref name="target"/> are also pinned automatically.
235
235
/// </summary>
236
236
/// <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>
239
242
public IDisposable DisableSaveChanges ( IEntity target )
240
243
{
241
244
EnsureNotDisposed ( ) ;
242
245
ArgumentValidator . EnsureArgumentNotNull ( target , "target" ) ;
246
+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) )
247
+ return null ; // No need to pin in this case
248
+
243
249
var targetEntity = ( Entity ) target ;
244
250
targetEntity . EnsureNotRemoved ( ) ;
245
- return Configuration . Supports ( SessionOptions . AutoSaveChanges )
246
- ? pinner . RegisterRoot ( targetEntity . State )
247
- : null ; // No need to pin in this case
251
+ return pinner . RegisterRoot ( targetEntity . State ) ;
248
252
}
249
253
250
254
/// <summary>
251
255
/// Temporarily disables only automatic save changes operations before queries, etc.
252
256
/// Explicit call of <see cref="SaveChanges"/> will lead to flush changes anyway.
253
257
/// If save changes is to be performed due to starting a nested transaction or committing a transaction,
254
258
/// 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"/>),
256
261
/// otherwise <see langword="null"/>.</returns>
257
262
/// </summary>
258
263
public IDisposable DisableSaveChanges ( )
259
264
{
260
- if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) ) {
261
- return null ; // No need to pin in this case
265
+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) || disableAutoSaveChanges ) {
266
+ return null ; // No need to pin in these cases
262
267
}
263
- if ( disableAutoSaveChanges )
264
- return null ;
265
268
266
269
disableAutoSaveChanges = true ;
267
270
return new Disposable ( _ => {
@@ -318,7 +321,7 @@ private void CancelEntitiesChanges()
318
321
newEntity . Update ( null ) ;
319
322
newEntity . PersistenceState = PersistenceState . Removed ;
320
323
}
321
-
324
+
322
325
foreach ( var modifiedEntity in EntityChangeRegistry . GetItems ( PersistenceState . Modified ) ) {
323
326
modifiedEntity . RollbackDifference ( ) ;
324
327
modifiedEntity . PersistenceState = PersistenceState . Synchronized ;
0 commit comments