To record entityvalidationerrors details

Source: Internet
Author: User

0. A question

People who have used EF believe they will encounter validation failed for one or more entities. See ' entityvalidationerrors ' This exception, which is due to an exception caused by EF validating the entity before writing to the database (if configuration is not set. Validateonsaveenabled=false words), because EF did not throw this exception details to the upper layer, often can not track to which property is not verified through, although know wrong, but the vegetarian still do not know where the wrong ah, how to change?

1. Record Entityvalidationerrors in DbContext

Now that you have EF eating entityvalidationerrors, I have to let it come back, first. 22 Code:

 Public classTestdbcontext:dbcontext { Public Override intSaveChanges () {Try            {                return Base.            SaveChanges (); }            Catch(Dbentityvalidationexception exception) {varErrormessages =exception. Entityvalidationerrors. SelectMany (Validationresult=validationresult.validationerrors). Select (M=m.errormessage); varFullerrormessage =string. Join (", ", errormessages); //Record Log//Log.error (fullerrormessage);                varExceptionmessage =string. Concat (Exception. Message,"Verify that the exception message is:", Fullerrormessage); Throw Newdbentityvalidationexception (Exceptionmessage, exception.            Entityvalidationerrors); }            //other exceptions throw to the top        }    }

Quite halal a few lines of code can get EF verification message, very cost-effective wood!

In the EF additions and deletions, the underlying enabling entity validation and logging is very necessary, especially the code to the line, if there is no log, the system is abnormal, simply can't start ...

To record entityvalidationerrors details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.