Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Validation failed for one or more entities. See entityvalidationerrors property for more details.

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 20-Apr-23 1:37am
v2
Comments
Mehdi Gholam 23-Jul-15 1:43am    
...and your question is?
OriginalGriff 23-Jul-15 2:01am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
Mukesh Pr@sad 23-Jul-15 2:13am    
post your model class in which you are getting validation error..!
ZurdoDev 23-Jul-15 8:18am    
See the errors for more details.

Use exception handler to get detailed error info as given below.

C#
try
{
    // Your code...
    // Could also be before try if you know the exception occurs in SaveChanges

    context.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}
 
Share this answer
 
C#
try
{
    // Your code...
    // Could also be before try if you know the exception occurs in SaveChanges

    context.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}

---AND---
Quote:
Error: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
C#
public partial class ContactDetail
    {
        [StringLength(10)]
        public string OfficeEmail { get; set; }
    }

solution: correct your Data Model according to your database SET fieldNames or length.
C#
public partial class ContactDetail
    {
        [StringLength(50)]
        public string OfficeEmail { get; set; }
    }
 
Share this answer
 
v2
Comments
CHill60 18-Feb-22 5:38am    
Please be careful - when I first saw this I thought you had just copied the existing solution. It was only when I was tidying up the formatting that I realised that you had provided the actual solution. Best to get some words in or explicitly mentioning that you are quoting something else
All good suggestions above. In fact, I solved my issue using Yogeesha's catch block above. It may be important to file this error away in your head, because you may run into this again. For me, I was passing a null value into a table field that had a NOT NULL declaration. So, really, I would have solved this if I had just looked at the table fields.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900