title: How do I keep validation rules inside of entity while using a view model inside controller?
body: <p>So I've got an entity in my <code>Models</code> directory:</p>
  
  <pre><code>public class Event
  {
      public int Id { get; set; }
  
      [Required, MaxLength(50), MinLength(3)]
      public string Name { get; set; }
  
      [Required, MaxLength(2000)]
      public string Description { get; set; }
  }
  </code></pre>
  
  <p>and I want to expose it to views using a viewModel:</p>
  
  <pre><code>public class BaseEventViewModel
  {
      public string Name { get; set; }
  
      [DataType(DataType.MultilineText)]
      public string Description { get; set; }
  }
  
  public class EventCreateViewModel : BaseEventViewModel
  {
  
  }
  </code></pre>
  
  <p>My reasoning behind this is that I want all the data validation to be done on the entity, and all the presentation stuff (such as rendering a text area) to be done on the view model. Then I can use however many view models I want to represent my entity, while maintaining data integrity.</p>
  
  <p>So I changed my controller to use the new view model:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(EventCreateViewModel viewModel)
      {
          if (ModelState.IsValid)
          {
              db.Events.Add(new Event
              {
                  Name = viewModel.Name,
                  Description = viewModel.Description
              });
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>However none of the entity validation gets done and I'm able to submit a blank form which raises a <code>DbEntityValidationException</code> exception.</p>
  
  <p>Presumably this is because <code>ModelState.IsValid</code> is working on the view model, not the entity that the view model represents. How can I catch these validation errors?</p>