Overriding SaveChanges and setting ModifiedDate, but how do I set ModifiedBy?

怎甘沉沦 提交于 2019-11-26 18:50:43

You can use the HttpContext.Current.User.Identity.Name to get the name of the current user.

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries<IAuditable>();

    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
        {
            entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
            entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name;
        }
    }
    return base.SaveChanges();
}

Better way to do this would be to use constructor injection to pass the current user to the context

public class MyContext : DbContext
{
    public MyContext(string userName)
    {
        UserName = userName;
    }

    public string UserName
    {
        get; private set;
    }

    public override int SaveChanges()
    {
       var changeSet = ChangeTracker.Entries<IAuditable>();

       if (changeSet != null)
       {
          foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
          {
              entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
              entry.Entity.ModifiedBy = UserName;
          }
       }
       return base.SaveChanges();
   }
}
Matthew

I also wanted to automate the population of audit fields on my MVC 4 / Entity Framework 5 application. I used information from @Eranga's answer and this blog: http://lourenco.co.za/blog/2013/07/audit-trails-concurrency-and-soft-deletion-with-entity-framework/ to make this approach work for me with Ninject - posting in case valuable to anyone else:

Created an interface and abstract class:

public interface IAuditableEntity {
    DateTime? CreatedDate { get; set; }
    string CreatedBy { get; set; }
    DateTime? LastModifiedDate { get; set; }
    string LastModifiedBy { get; set; }
}

public abstract class AuditableEntity:IAuditableEntity {
    public DateTime? CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTime? LastModifiedDate { get; set; }
    public string LastModifiedBy { get; set; }
}

Used them in my entities:

public class DataEntity : AuditableEntity {
    public int DataEntityID { get; set; }
    ...
}

Added a constructor to MyDbContext which accepted the HttpContext and overrode SaveChanges:

public EFDbContext(HttpContext context) {
    _context = context;
}

public HttpContext _context {
    get;
    private set;
}

public override int SaveChanges() {
    DateTime currentDateTime = DateTime.Now;

    foreach (var auditableEntity in ChangeTracker.Entries<IAuditableEntity>()) {
        if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified) {
            auditableEntity.Entity.LastModifiedDate = currentDateTime;
            switch (auditableEntity.State) {
                    case EntityState.Added:
                        auditableEntity.Entity.CreatedDate = currentDateTime;
                        auditableEntity.Entity.CreatedBy = _context.User.Identity.Name;
                        break;
                    case EntityState.Modified:
                        auditableEntity.Entity.LastModifiedDate = currentDateTime;
                        auditableEntity.Entity.LastModifiedBy = _context.User.Identity.Name;
                        if (auditableEntity.Property(p => p.CreatedDate).IsModified || auditableEntity.Property(p => p.CreatedBy).IsModified) {
                            throw new DbEntityValidationException(string.Format("Attempt to change created audit trails on a modified {0}", auditableEntity.Entity.GetType().FullName));
                        }
                        break;
                }
            }
        }
        return base.SaveChanges();
    }

Finally - need to have a DbContext per request and pass HttpContext as below based on this answer: https://stackoverflow.com/a/3617961/1803682 - note that as MyDbContext is now Request scope, the Repositories must be as well.

kernel.Bind<IDataRepository>()
      .To<EFDataRepository>()
      .InRequestScope();

kernel.Bind<MyDbContext>().ToSelf()
      .InRequestScope()
      .WithConstructorArgument("context", ninjectContext=>HttpContext.Current);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!