title: Database First and View Models
body: <p>I have little experience with Database First Models and how to approach these with MVC View Models.</p>
  
  <p>It is clear I cannot change the original model but I would like to add some annotations to the model for validation purposes.</p>
  
  <p>Therefore what I tried to do was create a ViewModel which inherits from the entity</p>
  
  <p>My DB First generated model - do not amend</p>
  
  <pre><code>public partial class xmldata
  {
      public string ISBN { get; set; }
      public string title { get; set; }
      public string blurb { get; set; }
      ...
  }
  </code></pre>
  
  <p>I then created a view model as follows which inherits from xmldata</p>
  
  <pre><code>public class XmlDataViewModel : xmldata
  {
      [AllowHtml]
      [Display(Name = "Blurb")]
      public string BlurbVm {
          get { return blurb; }
          set { blurb = value; }
      }
      ...
  }
  </code></pre>
  
  <p>The field shown above I needed to AllowHtml and my best solution so far but however in the controller actions I have still had to manually map the BlurbVm field back to blurb even though i would of thought the setter above would of handled that (im using Automapper for the rest of the fields) so I am confused as to why this didn't work.</p>
  
  <p>Also at the moment I am doing Validations in the controller and may want to refactor this later to move them into the View Model so I can use the [Required] annotation and also [Display (Name="Title")] fields which are currently handled in the view.</p>
  
  <p>Probably my overall question here is am I using the best strategy for dealing with Model Annotations and view models when using a Database First Model.</p>