locked
Validation failed for one or more entities. RRS feed

  • Question

  • User-162202432 posted

    Can someone help me with this error I am having?

    I get the message

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

    if (ModelState.IsValid)
                {
                    link.User = currentUser;
                    db.links.Add(link);
                    await db.SaveChangesAsync();
                    return RedirectToAction("Index");
                }
    

     

    Anyone can help me?

    Saturday, May 31, 2014 10:38 AM

Answers

  • User1957004874 posted

    await db.SaveChangesAsync();

    try catching the exception and go through the validation errors.

    try{
    db.SaveChangesAsync();
    }
    catch (DbEntityValidationException dbex)
    {
        foreach (var errors in dbex.EntityValidationErrors)
        {
            foreach (var error in errors.ValidationErrors)
            {
                 // Check both the below properties for errors.
                 // validationError.PropertyName, 
                 // validationError.ErrorMessage 
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 31, 2014 2:57 PM

All replies

  • User1957004874 posted

    await db.SaveChangesAsync();

    try catching the exception and go through the validation errors.

    try{
    db.SaveChangesAsync();
    }
    catch (DbEntityValidationException dbex)
    {
        foreach (var errors in dbex.EntityValidationErrors)
        {
            foreach (var error in errors.ValidationErrors)
            {
                 // Check both the below properties for errors.
                 // validationError.PropertyName, 
                 // validationError.ErrorMessage 
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 31, 2014 2:57 PM
  • User-162202432 posted

    How do I catch it. Where do I put that code?

    Saturday, May 31, 2014 4:44 PM
  • User1957004874 posted

    In your action method where you are trying to save.

    Saturday, May 31, 2014 6:59 PM