title: Entity Framework 6: audit/track changes
body: <p>I have my core project in C#.</p>
  
  <p>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".</p>
  
  <p>My question: is there a way to centralize this and make this data inserted automatically, where I create the instance of <code>dbContext</code>?</p>
  
  <p>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?</p>
  
  <p>EDIT:</p>
  
  <p>My attempt to override the saveChanges.</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE: I've summarized the <a href="https://programmingistheway.wordpress.com/2016/02/11/entity-framework-6-audittrack-changes/">solution here</a>.</strong></p>