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 ..