title: ASP.NET MVC 4 C# HttpPostedFileBase, EntityValidationErrors Save into DB
body: <p>In my database My FileLocation is VarChar(Max) I not sure what caused this EntityValidationErrors -System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. This error occurs when db.SaveChanges() function run;</p>
  
  <h1><strong>Model:</strong></h1>
  
  <pre><code>   public Assignment()
      {
          this.CourseAvailables = new HashSet&lt;CourseAvailable&gt;();
      }
  
      public string AssignmentID { get; set; }
      public Nullable&lt;System.DateTime&gt; SubmissionDate { get; set; }
      public string Status { get; set; }
      public Nullable&lt;decimal&gt; Mark { get; set; }
      public string Comments { get; set; }
      public string FileLocation  { get; set; }
  
      public virtual ICollection&lt;CourseAvailable&gt; CourseAvailables { get; set; }
  }
  </code></pre>
  
  <h1><strong>Controller:</strong></h1>
  
  <pre><code>        [HttpPost]
        public ActionResult Create(Assignment assignment)
      {
      if (ModelState.IsValid)
      {
  
          if (Request.Files.Count &gt; 0)
          {
              HttpPostedFileBase assignmentFile = Request.Files[0];
              if (assignmentFile.ContentLength &gt; 0)
              {
                  var fileName = Path.GetFileName(assignmentFile.FileName);
                  assignment.FileLocation = Path.Combine(Server.MapPath("~/Content/Image"), fileName);
                  assignmentFile.SaveAs(assignment.FileLocation);
              }
  
  
          }
              db.Assignments.Add(assignment);        
              db.SaveChanges();    
              return RedirectToAction("Index");
      }
      return View(assignment);
  }
  </code></pre>
  
  <h1><strong>View:</strong></h1>
  
  <pre><code>&lt;% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
    &lt;%: Html.TextBoxFor(model =&gt; model.FileLocation, new { type="file"})%&gt;
    &lt;%: Html.ValidationMessageFor(model =&gt; model.FileLocation) %&gt;
  </code></pre>