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

查看:186
本文介绍了实体框架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?

如果没有,我会用审计跟踪工具。我已经看到了其中的一些,但有一个问题:所有这些,都需要在我的模型的一些code。但我不希望在我的模型来写,因为如果我要改变它,我就失去了MODS。是否有可能使用EF6的审计线索没有在模型文件(S)写什么呢?怎么样?

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

更新:我已经总结了<一个href=\"https://programmingistheway.word$p$pss.com/2016/02/11/entity-framework-6-audittrack-changes/\">solution这里。

UPDATE: I've summarized the solution here.

推荐答案

如果使用EF6的的DbContext 您可以使用 ChangeTracker 的SaveChanges 覆盖查找添加的自定义类型/修改实体,例如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天全站免登陆