title: ASP MVC Database First - Lose all validations when refreshing EF Entity Framework
body: <p>I have this annoying problem were every time I want to refresh my Entity Framework schema (Database First), I lose all validation code I had written (with Resource for translation, etc...) and then I need to get back to my previous code from version control and overwrite it the way it was prior to refreshing schema. I did find a patch to overpass this problem but I find it's quite some overhead.... I need some suggestions on how to probably handle that..please!!! So let say my model with validation and resources (shorter for the question) is the following: </p>
  
  <pre><code>public partial class tblReport
  {
      public int id { get; set; }
  
      [Display(ResourceType = typeof(Resources.Admin), Name = "ReportName")]
      [Required(ErrorMessageResourceType = typeof(Resources.Admin), ErrorMessageResourceName = "Required")]
      public string reportName { get; set; }
  
      [Display(ResourceType = typeof(Resources.Admin), Name = "CreatedDate")]
      [Required(ErrorMessageResourceType = typeof(Resources.Admin), ErrorMessageResourceName = "Required")]
      public DateTime dtCreated { get; set; }
  }
  </code></pre>
  
  <p>After doing a refresh of the EF Schema, it will erase everything and put it back to default as the following:</p>
  
  <pre><code>public partial class tblReport
  {
      public int id { get; set; }
  
      public string reportName { get; set; }
  
      public DateTime dtCreated { get; set; }
  }
  </code></pre>
  
  <p>Now I did find a patch as I said, which is to extend the <code>tblReport</code> class and put all validations in there, but this equals to a copy of 1 to 1 from EF Schema and inserting validations... So I do an Extend as the following:</p>
  
  <pre><code>public class tblReportModel : tblReport
  {
      public int id { get; set; }
  
      [Display(ResourceType = typeof(Resources.Admin), Name = "ReportName")]
      [Required(ErrorMessageResourceType = typeof(Resources.Admin), ErrorMessageResourceName = "Required")]
      public string reportName { get; set; }
  
      [Display(ResourceType = typeof(Resources.Admin), Name = "CreatedDate")]
      [Required(ErrorMessageResourceType = typeof(Resources.Admin), ErrorMessageResourceName = "Required")]
      public DateTime dtCreated { get; set; }
  
      public tblReport ToTblReport()
      {
          tblReport tReport = new tblReport();
  
          tReport.dtCreated = this.dtCreated;
          tReport.reportName = this.reportName;
          tReport.id = this.id;
  
          return tReport;
      }
  
      public static tblReportModel ToTblReportModel(tblReport createdReport)
      {
          tblReportModel mReport = new tblReportModel();
  
          mReport.dtCreated = createdReport.dtCreated;
          mReport.reportName = createdReport.reportName;
          mReport.id = createdReport.id;
  
          return mReport;
      }
  }
  </code></pre>
  
  <p>I find it's so much work for nothing and not only that, I need to update all my code in my Controller and go against the new <code>tblReportModel</code> for the Create/Edit pages so that they get the validations done. I does work this way but it's so much overhead...oh and doing it this way, I also get a bunch of warnings saying</p>
  
  <pre><code>'IntranetApplication.Models.tblReportModel.dtCreated' hides inherited member 'IntranetApplication.Models.tblReport.dtCreated'. Use the new keyword if hiding was intended.
  </code></pre>
  
  <p>Please!!! Does anyone have a better solution than this??? </p>