@@ -229,7 +229,7 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
229
229
}
230
230
231
231
/// <summary>
232
- /// Temporarily disables all save changes operations (both explicit ant automatic)
232
+ /// Temporarily disables all save changes operations (both explicit ant automatic)
233
233
/// for specified <paramref name="target"/>.
234
234
/// Such entity is prevented from being persisted to the database,
235
235
/// when <see cref="SaveChanges"/> is called or query is executed.
@@ -239,34 +239,37 @@ private async ValueTask Persist(PersistReason reason, bool isAsync, Cancellation
239
239
/// all entities that reference <paramref name="target"/> are also pinned automatically.
240
240
/// </summary>
241
241
/// <param name="target">The entity to disable persisting.</param>
242
- /// <returns>A special object that controls lifetime of such behavior if <paramref name="target"/> was not previously processed by the method,
243
- /// otherwise <see langword="null"/>.</returns>
242
+ /// <returns>
243
+ /// A special object that controls lifetime of such behavior if <paramref name="target"/> was not previously processed by the method
244
+ /// and automatic saving of changes is enabled (<see cref="SessionOptions.AutoSaveChanges"/>),
245
+ /// otherwise <see langword="null"/>.
246
+ /// </returns>
244
247
public IDisposable DisableSaveChanges ( IEntity target )
245
248
{
246
249
EnsureNotDisposed ( ) ;
247
250
ArgumentValidator . EnsureArgumentNotNull ( target , "target" ) ;
251
+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) )
252
+ return null ; // No need to pin in this case
253
+
248
254
var targetEntity = ( Entity ) target ;
249
255
targetEntity . EnsureNotRemoved ( ) ;
250
- return Configuration . Supports ( SessionOptions . AutoSaveChanges )
251
- ? pinner . RegisterRoot ( targetEntity . State )
252
- : EmptyDisposable ; // No need to pin in this case
256
+ return pinner . RegisterRoot ( targetEntity . State ) ;
253
257
}
254
258
255
259
/// <summary>
256
260
/// Temporarily disables only automatic save changes operations before queries, etc.
257
261
/// Explicit call of <see cref="SaveChanges"/> will lead to flush changes anyway.
258
262
/// If save changes is to be performed due to starting a nested transaction or committing a transaction,
259
263
/// active disabling save changes scope will lead to failure.
260
- /// <returns>A special object that controls lifetime of such behavior if there is no active scope,
264
+ /// <returns>A special object that controls lifetime of such behavior if there is no active scope
265
+ /// and automatic saving of changes is enabled (<see cref="SessionOptions.AutoSaveChanges"/>),
261
266
/// otherwise <see langword="null"/>.</returns>
262
267
/// </summary>
263
268
public IDisposable DisableSaveChanges ( )
264
269
{
265
- if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) ) {
266
- return EmptyDisposable ; // No need to pin in this case
270
+ if ( ! Configuration . Supports ( SessionOptions . AutoSaveChanges ) || disableAutoSaveChanges ) {
271
+ return null ; // No need to pin in these cases
267
272
}
268
- if ( disableAutoSaveChanges )
269
- return null ;
270
273
271
274
disableAutoSaveChanges = true ;
272
275
return new Disposable ( _ => {
@@ -323,7 +326,7 @@ private void CancelEntitiesChanges()
323
326
newEntity . Update ( null ) ;
324
327
newEntity . PersistenceState = PersistenceState . Removed ;
325
328
}
326
-
329
+
327
330
foreach ( var modifiedEntity in EntityChangeRegistry . GetItems ( PersistenceState . Modified ) ) {
328
331
modifiedEntity . RollbackDifference ( ) ;
329
332
modifiedEntity . PersistenceState = PersistenceState . Synchronized ;
0 commit comments