title: How can I add default values to a property when saving using Code First EF4.1?
body: <p>I started by creating some models like this:</p>
  
  <pre><code>public abstract class EditableBase
  {
      public DateTime CreatedOn { get; set; }
      public DateTime ModifiedOn { get; set; }
  
      public int CreatedBy { get; set; }
      public int ModifiedBy { get; set; }
  }
  
  public class Project : EditableBase
  {
      public int ProjectId { get; set; }
      public string ProjectName { get; set; }
  }
  </code></pre>
  
  <p>And I use this line when the app starts:</p>
  
  <pre><code>Database.SetInitializer&lt;ModelContext&gt;(
              new DropCreateDatabaseIfModelChanges&lt;ModelContext&gt;());
  </code></pre>
  
  <p>A table called Projects is created with all the properties mentioned above as columns... this is exactly what I wanted.</p>
  
  <p>However, now I need populate some default values when I issue a SaveChanges() on DbContext.  When I save I need to update the ModifiedOn and ModifiedBy properties with the appropriate values.</p>
  
  <p>Normally I would at least do the DateTime values on the database side (either a trigger or a stored procedure) however this is obviously not an option here since the database will be dropped anytime a class changes.  And since I did code first I do not have a model designer that I can tweak the properties on.</p>
  
  <p>What I would like to do is add a method in the EditableBase class that gets called when the SaveChanges() is executed, thus keeping all the logic involved in one place.  Is it possible to do this?  What is the best way to achieve my goal?</p>