[RESOLVED]entity validation error on Posting

I am getting a validation error on post and the stack trace that it produces is not very helpful, is there a way I can get a more detailed error message?

For instance what I am getting just says

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

  System.Data.Entity.Internal.InternalContext.SaveChanges() +168
   System.Data.Entity.Internal.LazyInternalContext.SaveChanges() +26
   System.Data.Entity.DbContext.SaveChanges() +20

  [HttpPost]
        public ActionResult createpost()
        {

         
                // Add new thread     
                thread newthread = new thread();
                newthread.profileID = myID;
                newthread.submiton = DateTime.Now;
                newthread.activities = DateTime.Now;
                db.threads.Add(newthread);
                db.SaveChanges();
              }

The db.SaveChanges(); is highlighted on error,  but it does not tell which property is causing the error or why. The above code is summarized there are more fields, any suggestions would be great.

And what’s here 

castro305

See ‘EntityValidationErrors’ property for more details

?

Hi,

Use ‘try and catch’ to get the exact exception details. Inspect the inner exception property of exception object.  For clarity you can further inspect exception inside the Inner Exception property. After drill down through the inner exceptions finally you
will get the actual exception detail..

Hi castro305,

Thanks for your post.

You can try to use  the code as shown below to identify the root cause.

public ActionResult createpost()   
{   
    try   
   {  
//Add your code } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } }

This code above  helps you to trace the exact error.

More information, you can refer to the following link:

#Validation Failed For One or More Entities: MVC/Entity Framework 5.0

http://www.c-sharpcorner.com/UploadFile/97fc7a/validation-failed-for-one-or-more-entities-mvcentity-frame/

Hope this can be helpful.

Best Regards,

Eileen

Leave a Reply