20 comments

  1. This is incredibly useful and is making working threw Validation exceptions a LOT more straight forward.
    Thanks.

    VipX1 Dev.

  2. Great, thanks a lot.

    TheDutchGuy

  3. What if I want those Details to reach the client?

    I have EF context exposed through a WCF Data Service and I still get the formal ( no-details) DataServiceRequestException “An error occurred while processing this request”. What to do?

    Razvan

  4. This helped me out so much with a frustrating issue, thanks!

    Alex

  5. Thanks
    useful 🙂

    sameer

  6. Thank mate. That was awsome.

    Marian Pintilie

  7. Nice one Martin
    I’d been doing this every time I called Context.SaveChanges(), this makes it so simple.

    Thanks again.

    Mark Harby

  8. I’ll just say that, thank you very much for bring me this information. I usually don´t leave replies but now you give me an excellent tool for my project.

    Thank you again, I’m gonna put this webpage as the link for copyright into my code.

    Regards.

    Rodrigo Uzcátegui

  9. Excellent..
    Thanks very much for this blog.. very very helpful.
    It saved me hell lot of time.

    Regards

    Shafeeq

  10. […] entities. See ‘EntityValidationErrors’ property for more details” and found a blog post to follow which provided me the details I needed when debugging to solve the […]

    Template, Registration and Login | Devin W.C. Ryan

  11. Below is a version that includes all the necessary includes:

    using System.Data.Entity;
    using System.Data.Entity.Validation;
    using System.Linq;

    namespace MyNamespace
    {

    public partial class MyContext : DbContext
    {
    // Override base SaveChanges to expand out validation errors so client gets an actually helpful message
    public override int SaveChanges()
    {
    try
    {
    return base.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);
    }
    }
    }
    }
    </pre

    Sean

  12. Mastch… Liked it.. Thanx…

    TK

  13. What if I am making use of code first?

    Sunny Okoro

    • It works the same way!

      asdf

  14. Thanks so much !
    for almost a week I have been through that error.

    Mohamed Moka

  15. thanks a lot .. it is perfectly useful

    bharathiraja

  16. very useful i found the solution . now i’m very happy..

    Sathish

  17. Awesome!

    LB

  18. thanks

    anonymous

Comments are closed.