实体框架 6:审计/跟踪变更 [英] Entity Framework 6: audit/track changes

查看:31
本文介绍了实体框架 6:审计/跟踪变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的核心项目是 C#.

I have my core project in C#.

我在一个数据库上工作,其中一些表有列user_mod"和date_mod",用于标记谁和何时制作了一些模组,data_new"和user_new"也是如此.

I work on a database, where some tables have the columns "user_mod" and "date_mod" for sign who and when made some mods and the same with "data_new" and "user_new".

我的问题:有没有办法集中它并使这些数据自动插入,我在其中创建 dbContext 的实例?

My question: is there a way to centralize this and make this data inserted automatically, where I create the instance of dbContext?

如果没有,我将使用审计跟踪工具.我见过其中一些,但有一个问题:所有这些都需要在我的模型中添加一些代码.但我不想写在我的模型中,因为如果我必须更改它,我将丢失模型.是否可以在不写入模型文件的情况下对 EF6 使用审计跟踪?怎么样?

If not, I will use an audit trail tool. I have seen some of these, but there is a problem: all of these, require some code in my model. But I don't want to write in my model, because if I have to change it, I will lost the mods. Is it possible use an audit trail for EF6 without writing in the model file(s)? How?

我试图覆盖 saveChanges.

My attempt to override the saveChanges.

public partial class PieEntities : DbContext
{
    public override int SaveChanges(System.Data.Objects.SaveOptions options)
    {
        var timestamp = DateTime.Now;

        EntityState es = EntityState.Added;
        ObjectStateManager o = new ObjectStateManager();

        foreach (ObjectStateEntry entry in o.GetObjectStateEntries(EntityState.Added ))  {
            if (entry.Entity.GetType() == typeof(TabImpianti)) {
                TabImpianti impianto = entry.Entity as TabImpianti;
                impianto.DATA_INS = timestamp;
                impianto.DATA_MOD = timestamp;
                string u = mdlImpostazioni.p.UserName;
                impianto.USER_INS = u;
                impianto.USER_MOD = u;
            }
        }
        return base.SaveChanges(options);
    }
}

更新:我在这里总结了解决方案.

推荐答案

如果使用 EF6 的 DbContext 你可以使用 ChangeTrackerSaveChanges 覆盖查找自定义类型的添加/修改实体,例如 IAuditedEntity.

If using EF6's DbContext you can use ChangeTracker in SaveChanges override to find added/modified entities of custom type, for example IAuditedEntity.

public interface IAuditedEntity {
  string CreatedBy { get; set; }
  DateTime CreatedAt { get; set; }
  string LastModifiedBy { get; set; }
  DateTime LastModifiedAt { get; set; }
}

public override int SaveChanges() {
  var addedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
    .Where(p => p.State == EntityState.Added)
    .Select(p => p.Entity);

  var modifiedAuditedEntities = ChangeTracker.Entries<IAuditedEntity>()
    .Where(p => p.State == EntityState.Modified)
    .Select(p => p.Entity);

  var now = DateTime.UtcNow;

  foreach (var added in addedAuditedEntities) {
    added.CreatedAt = now;
    added.LastModifiedAt = now;
  }

  foreach (var modified in modifiedAuditedEntities) {
    modified.LastModifiedAt = now;
  }

  return base.SaveChanges();
}

这篇关于实体框架 6:审计/跟踪变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆