title: Entity Framework - Data Annotations Range Validator throws exception instead of displaying error message
body: <p>I'm trying to create a form about vendor registration in a company. One of the field required is Number of Workers.</p>
  
  <p>I added Range validator in Form Model as below.</p>
  
  <pre><code>[Key]
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  [Required(ErrorMessage = "Form Number is required!")]
  public int FormNo { get; set; }
  
  [Required(ErrorMessage = "Please fill in the Number of Workers field!")]
  [Range(0, 20, ErrorMessage = "Please enter positive number only!")]
  public int NWorkers { get; set; }
  </code></pre>
  
  <p>Every time the input is outside the range, it throws exception instead of displaying the ErrorMessage.</p>
  
  <pre><code>System.Data.Entity.Validation.DbEntityValidationException: 'Validation
  failed for one or more entities. See 'EntityValidationErrors' property
  for more details.'
  </code></pre>
  
  <p>If the input is within the range, it works fine.</p>
  
  <p>Here is my controller</p>
  
  <pre><code>public class FormController : Controller {
      private AppDbContext db = new AppDbContext();
  
      // GET: Form
      public ActionResult Index() {
          return View(db.Forms.ToList());
      }
  
      [HttpGet]
      public ActionResult Create() {
          return View();
      }
  
      [HttpPost]
      public ActionResult Create(Form form) {
          form.CreatedAt = DateTime.Now;
          form.UpdatedAt = DateTime.Now;
  
          db.Forms.Add(form);
          db.SaveChanges();
  
          return RedirectToAction("index", "form");
      }
  }
  </code></pre>
  
  <p>Here is my view</p>
  
  <pre><code>&lt;form class="form-horizontal" action="@Url.Action("create", "form")" method="post"&gt;
     @if (!ViewData.ModelState.IsValid) {
        &lt;div class="alert alert-danger"&gt;
           @Html.ValidationSummary(false, "")
        &lt;/div&gt;
     }
  
     &lt;div class="form-group"&gt;
        &lt;label class="col-md-3 control-label" for="FormNo"&gt;Form Number&lt;/label&gt;
        &lt;div class="col-md-3"&gt;
           @Html.TextBox("FormNo", null, new { @class = "form-control required", @required = "true" })
        &lt;/div&gt;
        &lt;span class="col-md-3 pull-right note"&gt;Note : * is required&lt;/span&gt;
     &lt;/div&gt;
  
     &lt;div class="form-group"&gt;
        &lt;label class="col-md-3 control-label" for="NWorkers"&gt;Number of Workers&lt;/label&gt;
        &lt;div class="col-md-3 required"&gt;
           @Html.TextBox("NWorkers", null, new { @class = "form-control" })
           &lt;!-- &lt;input type="text" class="form-control" id="NWorkers" name="NWorkers" value="0" /&gt; --&gt;
        &lt;/div&gt;
     &lt;/div&gt;
  
     &lt;div class="box-footer"&gt;
        &lt;div class="col-md-offset-3"&gt;
           &lt;button type="submit" class="btn btn-primary"&gt;
              &lt;i class="fa fa-btn fa-floppy-o"&gt;&lt;/i&gt; Save
           &lt;/button&gt;
           &lt;a onclick="history.back();" class="btn btn-warning"&gt;
              &lt;i class="fa fa-reply"&gt;&lt;/i&gt; Cancel
           &lt;/a&gt;
        &lt;/div&gt;
     &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p><a href="https://i.stack.imgur.com/obzMk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/obzMk.png" alt="Error Details"></a></p>
  
  <p>StackTrace</p>
  
  <blockquote>
    <p>"   at System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n<br>
    at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n<br>
    at System.Data.Entity.DbContext.SaveChanges()\r\n   at
    HSE.Controllers.FormController.Create(Form form) in
    C:\Users\PC17A196\source\repos\HSE\HSE\Controllers\FormController.cs:line
    28\r\n   at lambda_method(Closure , ControllerBase , Object[] )\r\n<br>
    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase
    controller, Object[] parameters)\r\n   at
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
    controllerContext, IDictionary<code>2 parameters)\r\n   at
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext
    controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2
    parameters)\r\n   at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult
    asyncResult, ActionInvocation innerInvokeState)\r\n   at
    System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<code>2.CallEndDelegate(IAsyncResult
    asyncResult)\r\n   at
    System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase</code>1.End()\r\n
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult
    asyncResult)\r\n   at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()\r\n
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;>c__DisplayClass46.b__3f()"    string</p>
  </blockquote>