Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

c# - While saving some Client note, am getting this error " See 'EntityValidationErrors' property for more details"

Hi, am trying save the Client Note but it's showing this error, please can someone help me to resolve this. Am using a notes page in my application, it will save until certain words. If am crossing that and trying to save, it's showing the error as"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this chunk of code, this will tell you exactly on what column of which entity validation is failing, and you will eaisly reach the place where its creating problem.

 try
 {
     db.SaveChanges();
 }
 catch (DbEntityValidationException ex)
 {
     // Retrieve the error messages as a list of strings.
     var errorMessages = ex.EntityValidationErrors
                           .SelectMany(x => x.ValidationErrors)
                           .Select(x => x.ErrorMessage);

     // Join the list to a single string.
     var fullErrorMessage = string.Join("; ", errorMessages);

     // Combine the original exception message with the new one.
     var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

    // Throw a new DbEntityValidationException with the improved exception message.
   throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...