public override Task <int> SaveChangesAsync(CancellationToken cancellationToken) { var op = _currentSaveOperation; if (op != null) { if (op.Stage == SaveStage.PreSave) { // // This was called from within a PRE action hook. We must get out:... // // 1.) to prevent cyclic calls // // 2.) we want new entities in the state tracker (added by pre hooks) to be committed atomically in the core SaveChanges() call later on. return(Task.FromResult(0)); } else if (op.Stage == SaveStage.PostSave) { // // This was called from within a POST action hook. Core SaveChanges() has already been called, // // but new entities could have been added to the state tracker by hooks. // // Therefore we need to commit them and get outta here, otherwise: cyclic nightmare! return(SaveChangesCoreAsync(cancellationToken)); } } _currentSaveOperation = new SaveChangesOperation(this, this.DbHookHandler); using (new ActionDisposable(() => _currentSaveOperation = null)) { return(_currentSaveOperation.ExecuteAsync(cancellationToken)); } }
public override int SaveChanges() { var op = _currentSaveOperation; if (op != null) { if (op.Stage == SaveStage.PreSave) { // This was called from within a PRE action hook. We must get out:... // 1.) to prevent cyclic calls // 2.) we want new entities in the state tracker (added by pre hooks) to be committed atomically in the core SaveChanges() call later on. return(0); } else if (op.Stage == SaveStage.PostSave) { // This was called from within a POST action hook. Core SaveChanges() has already been called, // but new entities could have been added to the state tracker by hooks. // Therefore we need to commit them and get outta here, otherwise: cyclic nightmare! // DetectChanges() here is important, 'cause we turned it off for the save process. base.ChangeTracker.DetectChanges(); return(SaveChangesCore()); } } _currentSaveOperation = new SaveChangesOperation(this, this.DbHookHandler); Action endExecute = () => { _currentSaveOperation?.Dispose(); _currentSaveOperation = null; }; using (new ActionDisposable(endExecute)) { return(_currentSaveOperation.Execute()); } }
public override Task <int> SaveChangesAsync(CancellationToken cancellationToken) { var op = _currentSaveOperation; if (op != null) { if (op.Stage == SaveStage.PreSave) { // This was called from within a PRE action hook. We must get out:... // 1.) to prevent cyclic calls // 2.) we want new entities in the state tracker (added by pre hooks) to be committed atomically in the core SaveChanges() call later on. return(Task.FromResult(0)); } else if (op.Stage == SaveStage.PostSave) { // This was called from within a POST action hook. Core SaveChanges() has already been called, // but new entities could have been added to the state tracker by hooks. // Therefore we need to commit them and get outta here, otherwise: cyclic nightmare! // DetectChanges() here is important, 'cause we turned it off for the save process. base.ChangeTracker.DetectChanges(); return(SaveChangesCoreAsync(cancellationToken)); } } _currentSaveOperation = new SaveChangesOperation(this, this.DbHookHandler); var result = _currentSaveOperation.ExecuteAsync(cancellationToken); result.ContinueWith(t => { _currentSaveOperation?.Dispose(); _currentSaveOperation = null; }); return(result); }