title: in Entity framework, how to call a method on Entity before saving
body: <p>Below I have created a demo entity to demonstrate what I'm looking for:</p>
  
  <pre><code>public class User :  IValidatableObject
  {
      public string Name { get; set; }
  
      [Required]
      public DateTime CreationDate { get; set; }
  
      public DateTime UpdatedOnDate { get; set; }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          if(Name = "abc")
          {
              yield return new ValidationResult("please choose any other name then abc", new[] { "Name" });
          }
      }
  }
  </code></pre>
  
  <p>I am implementing <code>IValidatableObject</code> interface to make this entity SelfValidating.</p>
  
  <p>Now currently to create new User iam doing this</p>
  
  <pre><code>User u = new User();
  u.Name = "Some name";
  u.CreationDate = DateTime.Now
  dbContext.Users.Add(u);
  dbContext.SaveChanges();
  </code></pre>
  
  <p>Iam planning to shift <code>u.CreationDate=DateTime.Now;</code> code inside <code>User</code> class. And implement an interface that provides a method which will be executed before saving and after validating</p>
  
  <pre><code>// class structure that I am looking for
  public class User : IValidatableObject,IMyCustomInterFace
  {
      //rest codes as above class
  
      public void MyMethod(Whatever)
      {
          //this method gets called after Validate() and before save
  
          if(dataContext.Entry&lt;User&gt;(this).State == System.Data.EntityState.Added)
          {
              //add creation date_time
              this.CreationDate=DateTime.Now;
  
              //SET MORE DEFAULTS
          }
  
          if(dataContext.Entry&lt;User&gt;(this).State == System.Data.EntityState.Modified)
          {
              //update Updation time
              this.UpdatedOnDate = DateTime.Now;
          }
      }
  }
  </code></pre>
  
  <p>now to create a new user I just have to do as below, note that I didn't added date property this time, Class does that automatically.</p>
  
  <pre><code>User u = new User();
  u.Name = "Some name";
  dbContext.Users.Add(u);
  dbContext.SaveChanges();
  </code></pre>
  
  <p>To update user, <code>UpdatedOnDate</code> property will be automatically updated by class</p>
  
  <pre><code>User u = getUserFromSomeWhere();
  u.Name = "Updated Name";
  dataContext.Entry&lt;User&gt;(u).State = System.Data.EntityState.Modified;
  dbContext.SaveChanges();
  </code></pre>
  
  <p><strong>My Question</strong>: is there any existing interface that provides 
  some method that gets called before <code>Save</code> and <code>AfterValidate</code> or some other ways of doing this, that I may not be knowing.</p>
  
  <p>Or, if I create my custom interface, how can I make its method to get executed in the order I want.</p>