title: By default how to insert created date each time when new record is inserted into database in mvc 4 entity framework
body: <p>Whenever I tried to insert data into database i am not able to get current date and in view I am hiding that particular field. Now whenever i insert some data to database created date should automatically inserted. </p>
  
  <p>This is my savechanges method.</p>
  
  <pre><code>public override int SaveChanges()
   {
    var entities = ChangeTracker.Entries()
     .Where(x =&gt; x.Entity is DocumentMaster &amp;&amp;
     (x.State == EntityState.Added || x.State == System.Data.EntityState.Modified));
  
     var currentUsername = "Anonymous";
     foreach (var entity in entities)
      {
       if (entity.State == EntityState.Added)
        {
         ((DocumentMaster)entity.Entity).CreatedDate = DateTime.Now;
         ((DocumentMaster)entity.Entity).CreatedBy = currentUsername;
        }
      }
       return base.SaveChanges();
      }
    }
  </code></pre>
  
  <p>This is my interface.</p>
  
  <pre><code>interface DocumentMaster
      {
          string CreatedBy { get; set; }
          DateTime CreatedDate { get; set; }
      }
  </code></pre>
  
  <p>And this is my class</p>
  
  <pre><code>public partial class ts_upld_doc
      {
          public ts_upld_doc()
          {
              this.tr_upld_content = new HashSet&lt;tr_upld_content&gt;();
          }
  
          public int upld_docid { get; set; }
          public int usr_createdby { get; set; }
      public Nullable&lt;System.DateTime&gt; upld_createddate { get; set; }
  }
  </code></pre>
  
  <p>Do i need ti implement interface in class? I tried to implement but It did not work. Can anyone suggest me on this?</p>