/// <summary>Audits and saves all changes made in this context to the underlying database.</summary> /// <param name="context">The context used to audits and saves all changes made.</param> /// <param name="audit">The audit to use to add changes made to the context.</param> /// <returns>The number of objects written to the underlying database.</returns> public static int SaveChanges(this DbContext context, Audit audit) { audit.PreSaveChanges(context); var rowAffecteds = context.SaveChanges(); audit.PostSaveChanges(); if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null) { audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit); context.SaveChanges(); } return rowAffecteds; }
/// <summary>A DbContext extension method that saves the changes asynchronous.</summary> /// <param name="context">The context used to audits and saves all changes made.</param> /// <param name="audit">The audit to use to add changes made to the context.</param> /// <param name="cancellationToken">A CancellationToken to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous save operation. The task result contains the number of objects written /// to the underlying database /// </returns> public static async Task<int> SaveChangesAsync(this DbContext context, Audit audit, CancellationToken cancellationToken) { audit.PreSaveChanges(context); var rowAffecteds = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); audit.PostSaveChanges(); if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null) { audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit); await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } return rowAffecteds; }
/// <summary>A DbContext extension method that saves the changes asynchronous.</summary> /// <param name="context">The context used to audits and saves all changes made.</param> /// <param name="audit">The audit to use to add changes made to the context.</param> /// <param name="cancellationToken">A CancellationToken to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous save operation. The task result contains the number of objects written /// to the underlying database /// </returns> public static async Task <int> SaveChangesAsync(this DbContext context, Audit audit, CancellationToken cancellationToken) { audit.PreSaveChanges(context); var rowAffecteds = await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); audit.PostSaveChanges(); if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null) { audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit); await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); } return(rowAffecteds); }
/// <summary>Audits and saves all changes made in this context to the underlying database.</summary> /// <param name="context">The context used to audits and saves all changes made.</param> /// <param name="audit">The audit to use to add changes made to the context.</param> /// <returns>The number of objects written to the underlying database.</returns> public static int SaveChanges(this DbContext context, Audit audit) { audit.PreSaveChanges(context); var rowAffecteds = context.SaveChanges(); audit.PostSaveChanges(); if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null) { audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit); context.SaveChanges(); } return(rowAffecteds); }