0

My Add/Delete functions all work correctly. However when I edit I get a validation error message from Entity.

The error is: {"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."}

Upon looking at EntityValidationErrors it only states: {System.Data.Entity.Validation.DbEntityValidationResult}

I am passing the model from my edit page to a Post/Edit method stub.

   [HttpPost]
   public ActionResult Edit(EmployeeRoadsideAssistance employee)
   {
       AlphaDBEntities db = new AlphaDBEntities();

       db.Entry(employee).State = EntityState.Modified;
       db.SaveChanges();

       return RedirectToAction("Edit", new { id = employee.id });
   }

It fails on save changes. The problem also exits if I make no changes to the actual model. So if I add the new person, then click edit, then click edit without making any changes I still get the error.

EDIT

Thanks to the wonderful comment by DavidG I was able to see the real error which stated that I was trying to pass a null to a required column.

This was because I pre-fill these during the "Add" step and so the "Edit" step doesn't have them in the model. I added hidden fields to accommodate the pre-filled columns and it works now.

James Wilson
  • 5,074
  • 16
  • 63
  • 122
  • 2
    Unfortunately it's impossible to read the errors in teh debug view, but try this link as an example of how to do it. If you can give us the real error, we can help. http://stackoverflow.com/a/10676526/1663001 – DavidG Apr 23 '15 at 15:57
  • @DavidG Thanks, that helped me solve it. It was missing a field it required to be saved. I wish I could give you credit somehow?! – James Wilson Apr 23 '15 at 16:02
  • No credit needed! :) – DavidG Apr 23 '15 at 16:03
  • Even if you already found out the answer, please post the code for the model in your question. Validation errors are usually due to data annotation attributes in the model not being respected. – julealgon Apr 23 '15 at 16:04

1 Answers1

1

To help you find the problem, you can wrap your db.SaveChanges() into a try catch and put a breakpoint on the catch line. This will allow you to naviguate into the exception and see the EntryType and the ValidationErrors.

try {    
    db.SaveChanges();
}
catch(DbEntityValidationException e) {
    throw;
}

This is how I fixed my EntityValidation problems. Most of the time this error is caused by a field validation present in your model.

Paradisee
  • 11
  • 3
  • I cant see how this can be a solution. You are just catching an exception. – Yogiraj Apr 23 '15 at 16:03
  • It isn't just an exception, it's the exception! What @Paradisee provided is better than just a solution, he or she provided the OP with a tool which will guide the OP to resolve any future entity validation exceptions. – Aydin Apr 23 '15 at 16:23
  • @AydinAdn Thanks. The provided information from OP is limited and not enough for me to see where the error come from. This is why I provided this trick so without looking further inside his structure he can find the source of the problem real quick. – Paradisee Apr 23 '15 at 17:23