Decrease Font Size
Increase Font Size
   BLOG

VB.NET Validation failed for one or more entities

Recently I was working on a VB.NET MVC application and run into the error 'Validation failed for one or more entities. See 'EntityValidationErrors' property for more details’. The error message didn’t provide much information and I found tons of solution in c# on how to display the error message in details. If you happen to run into this situation, here is the code snippet in VB.NET.

Try
   context.SaveChanges()
Catch ex As Entity.Validation.DbEntityValidationException
   For Each eve As Entity.Validation.DbEntityValidationResult In ex.EntityValidationErrors
                Debug.WriteLine(String.Format("Entity of type {0} in state {1} has the following validation errors:", _
                                   eve.Entry.Entity.GetType().Name, eve.Entry.State))
        For Each ve As Entity.Validation.DbValidationError In eve.ValidationErrors
            Debug.WriteLine(ve.PropertyName & " - " & ve.ErrorMessage)
        Next
   Next
End Try