0

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;

Model:

   public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation  { get; set; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}

Controller:

        [HttpPost]
      public ActionResult Create(Assignment assignment)
    {
    if (ModelState.IsValid)
    {

        if (Request.Files.Count > 0)
        {
            HttpPostedFileBase assignmentFile = Request.Files[0];
            if (assignmentFile.ContentLength > 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);
}

View:

<% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
  <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
  <%: Html.ValidationMessageFor(model => model.FileLocation) %>
WeakTaenie
  • 247
  • 2
  • 6
  • 14
  • 1
    When the `DbEntityValidationException` pops up, open the `inner exception` node. The real cause of a problem in EF is stored in inner exception not in the top layer exception. more info: http://stackoverflow.com/a/16347943/298573 – VahidN Aug 05 '14 at 05:47
  • Thanks alot, System.Data.SqlClient.SqlException: String or binary data would be truncated.The statement has been terminated. then may I know what atually caused this error? – WeakTaenie Aug 05 '14 at 06:06
  • try saving relative path in DB: ``assignment.FileLocation = Server.MapPath("~/Content/Image") + fileName;`` – Ehsan Sajjad Aug 05 '14 at 06:12
  • if you know that directory will always be same then you can only save file name: ``assignment.FileLocation = fileName;`` and when getting : ``Path.Combine(Server.MapPath("~/Content/Image/"), fileName)`` – Ehsan Sajjad Aug 05 '14 at 06:15
  • `String or binary data would be truncated` means you have a DB field with length=5 for instance and you are saving a value with length=5+n (greater than 5). Increase the length of the field. – VahidN Aug 05 '14 at 07:52

2 Answers2

3

Does it mention which column would have the data truncated? The data you're trying to save into that column is too long, that is, longer than the defined maximum length.

animuson
  • 53,861
  • 28
  • 137
  • 147
David Hartley
  • 130
  • 1
  • 5
0

Check your database and make sure your other columns are configured appropriately. Status, Comments, and Mark could all be causing problems if the data type and / or data length have not been set up correctly for their respective columns in the database.

Teppic
  • 2,506
  • 20
  • 26