title: How to automatically set DateCreated and DateUpdated
body: <p>If I have the following entity:</p>
  
  <pre><code>public class PocoWithDates {
      public string PocoName { get; set; }
      public DateTime DateCreated { get; set; }
     public DateTime DateUpdated { get; set; }
  }
  </code></pre>
  
  <p>corresponding to an SQL Server 2008 (R2) table with the same name/attributes...</p>
  
  <p>How can I automatically:
  - Set the <code>DateCreated</code> and <code>DateUpdated</code> field to now when doing an INSERT
  - Set the <code>DateUpdated</code> field to now when doing an UPDATE</p>
  
  <p>When I say automatically, I mean I want to be able to do this:</p>
  
  <pre><code>poco.Name = "Changing the name";
  repository.Save(); 
  </code></pre>
  
  <p>Not this:</p>
  
  <pre><code>poco.Name = "Changing the name";
  poco.LastModified = DateTime.Now;
  repository.Save();
  </code></pre>
  
  <p>Behind the scenes, "something" should automatically update the DateTime fields. What is that "something"? I'm using Entity Framework 6.0. Is there a way that EF can do that automatically? I know MySQL can do something like this. Or using triggers/stored procedures in SQL Server. But I do NOT want that.</p>
  
  <p>This question is an alternative and altered version of this question:
  <a href="https://stackoverflow.com/questions/3879011/entity-framework-sql2008-how-to-automatically-update-lastmodified-fields-for-e">Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities?</a></p>
  
  <p>This question got some great solutions/answers for EF 4.0. But I want it for EF6.0 with a <code>DBContext</code>. If I had more time I would try and adapt those solutions and get them working in EF6.0. But for now I'm wondering if anybody solved this already, or knows how to transcribe the custom <code>SaveChanges()</code> or <code>Add()</code> overrides for EF4.0 to EF6.0.</p>