-4

I am trying to update my data in database. I tried to update the Name and Order in the database. However, I can't update my database. The error is

DbEntityValidationException was unhandled by user code.

How can I solve this?

The following code is my controller

public ActionResult Update(Person[] People)
{
    for (int i = 0; i < People.Length; i++)
    {
        Person person = db.Persons.Find(People[i].ID);
        person.Order = People[i].Order;
        person.Name = People[i].Name;
        db.Entry(person).State = EntityState.Modified;
        db.SaveChanges();
    }


    return Json(new { JsonResult = "NICE" });
}
Ivar
  • 6,138
  • 12
  • 49
  • 61

1 Answers1

2

As I said in my comment this exception is more like a wrapper, you could loop through it in your error handler pulling out all of the validation errors

Something like this

var sb = new StringBuilder();
DbEntityValidationException validationException = (DbEntityValidationException)exception;
foreach (var e in validationException.EntityValidationErrors)
{
    foreach (var err in e.ValidationErrors)
    {
        sb.AppendLine($"Validation Error:{err.ErrorMessage}, Property: {err.PropertyName}");
    }
}
Lotok
  • 4,517
  • 1
  • 34
  • 44