Monday 18 June 2012

Error: Validation failed for one or more entities. See 'Entity ValidationErrors' property

Entity Framework 4 - Entity ValidationErrors.


I have came across my application where I got nice little error:

 Error: Validation failed for one or more entities. See 'Entity ValidationErrors' property

When I read the exception it has told me nothing but there is an error.
I have wrapped my data access with following code below to find out what is going on.




try{
       
 dbContext.Users.Add(user);
 dbContext.SaveChanges();


}catch (DbEntityValidationException dbEx)
{
 foreach (var validationErrors in dbEx.EntityValidationErrors)
 {
   foreach (var validationError in validationErrors.ValidationErrors)
    {
      Trace.TraceInformation(
        "Property: {0} Error: {1}",
        validationError.PropertyName,
        validationError.ErrorMessage);
   }
 }
}


The source of this code is here:
http://stackoverflow.com/questions/5400530/validation-failed-for-one-or-more-entities-while-saving-changes-to-sql-server-da

No comments:

Post a Comment