EF 6 DbContext覆盖AsyncSaveChanges

时间:2014-04-14 14:58:23

标签: entity-framework entity-framework-6 entity-framework-6.1

我有一个使用Entity Framework 6的Web API项目。

在我的DbContext文件中,我通常会覆盖 SaveChanges 方法,以便执行我的审核日志过程,如下所示:

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();
}

现在,我想在Entity Framework 6中使用Async功能,因此我需要覆盖SaveChangesAsync方法,但无法找到如何执行此操作的任何示例。

有谁知道如何覆盖SaveChangesAsync方法。?

0 个答案:

没有答案