Error in data insertion of Entity Framework: Validation failed for one or more entities

Source: Internet
Author: User
Tags foreach object model


When processing the Entity Framework to insert a database today, an error is returned:

Validation failed for one or more entities. See 'entityvalidationerrors 'property for more details.

I have been using Exception ex all the time. This general Exception is handled, and there is almost no error at all. There is no problem with the comparison between the object model and the database table. (EF was used at the beginning. It was previously written by a colleague. I only use the front-end );

1. Solution Step 1:

However, follow the prompt "See 'entityvalidationerrors 'property for more details." to view the Exception, you can only See

 

It cannot be seen why the property verification fails. This may be the case for many people.

Here we will introduce an Exception class, so that we can easily know which field has a problem.

That is System. Data. Entity. Validation. DbEntityValidationException. I believe the code knows how to write it. The simplest thing is

Try {// write database} catch (DbEntityValidationException dbEx ){}


In dbEx, we can see

 

In this way, we can see the details of all ValidationErrors of EntityValidationErrors.

 

2. Remove data that is dependent on the inserted database:

 

My object A has an attribute of public List <Shop> ShopList {get; set. The error message indicates that the properties of the Shop object are null. The shoplist data is not used here. In this way, this attribute is set to null in the service method and will not be verified.

Public tb_WeiXinCard AddCard (tb_WeiXinCard Card) {using (PathAnalysisContext da = new PathAnalysisContext () {string strSql = ""; // if (type = 1) // {// strSql = string. format ("delete from tb_shopcard WHERE AgentId = {0}", id); //} StringBuilder sb = new StringBuilder (); list <int> listint = new List <int> (); foreach (Shop sp in Card. shopList) {listint. add (sp. id);} // Card. shopList = null; Card = da. tb_WeiXinCard.Add (Card); da. saveChanges (); foreach (int sp in listint) {sb. append (string. format ("insert into tb_shopcard ([cardid], [shopid]) VALUES ({0 },{ 1});", Card. id, sp);} strSql = sb. toString (); da. database. executeSqlCommand (strSql); return Card ;}}


A simple record ..

 

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.