/// <summary> /// Asynchronously saves all changes made in this context to the underlying database. /// If the tracker for an entity is active, it will also put the old values in tracking table. /// </summary> /// <param name="userName">Username of the logged in identity</param> /// <param name="cancellationToken"> /// A System.Threading.CancellationToken to observe while waiting for the task /// to complete. /// </param> /// <returns>Returns the number of objects written to the underlying database.</returns> public virtual async Task <int> SaveChangesAsync(object userName, CancellationToken cancellationToken) { if (!GlobalTrackingConfig.Enabled) { return(await base.SaveChangesAsync(cancellationToken)); } if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } CommonTracker.AuditChanges(this, userName); IEnumerable <DbEntityEntry> addedEntries = CommonTracker.GetAdditions(this); // Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining. int result = await base.SaveChangesAsync(cancellationToken); //By now., we have got the primary keys of added entries of added entiries because of the call to savechanges. CommonTracker.AuditAdditions(this, userName, addedEntries); //save changes to audit of added entries await base.SaveChangesAsync(cancellationToken); return(result); }
/// <summary> /// This method saves the model changes to the database. /// If the tracker for an entity is active, it will also put the old values in tracking table. /// Always use this method instead of SaveChanges() whenever possible. /// </summary> /// <param name="userName">Username of the logged in identity</param> /// <returns>Returns the number of objects written to the underlying database.</returns> public virtual int SaveChanges(object userName) { CommonTracker.AuditChanges(this, userName); IEnumerable<DbEntityEntry> addedEntries = CommonTracker.GetAdditions(this); // Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining. int result = base.SaveChanges(); //By now., we have got the primary keys of added entries of added entiries because of the call to savechanges. CommonTracker.AuditAdditions(this, userName, addedEntries); //save changes to audit of added entries base.SaveChanges(); return result; }