1

I have a Web API project that uses Entity Framework 6.

Inside my DbContext file I normally override the SaveChanges method in order to do my Audit Log procedure, like this:

public override int SaveChanges() {

     throw new InvalidOperationException("User ID must be provided");

 }

public int SaveChanges(int userId, Guid sessionUid) {
    // Get all Added/Deleted/Modified entities (not Unmodified or Detached)
    foreach(var ent in this.ChangeTracker.Entries().Where(p = > p.State == System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted || p.State == System.Data.EntityState.Modified)) {
        // For each changed record, get the audit record entries and add them
        foreach(AuditLog x in GetAuditRecordsForChange(ent, userId, sessionUid)) {
            this.AuditLogs.Add(x);
        }
    }

    // Call the original SaveChanges(), which will save both the changes made and the audit records
    return base.SaveChanges();
}

Now, I would like to user Async feature in Entity Framework 6, so I need to override the SaveChangesAsync method but can't find any example of how to do that.

Does anyone know how to override the SaveChangesAsync method.?

4

0 回答 0