Because it is a reprint of the article The source is indicated here, please forgive me if there are articles that have been transferred before without indication, because some of them can no longer find the source, or for other reasons.
If you are offended, please contact me, or delete it, or indicate the source.
Because I used to just want to collect good articles, but sometimes the connection will fail, so now I go directly to myself when I encounter a good one.
Original source http://blog.csdn.net/snowinfish/article/details/11567301
When writing data to the ADO.NET entity model in VS2012, it keeps saying "DbEntityValidationException not handled" and "Validation of one or more entities failed. For more information, see EntityValidationErrors Properties.
Tested the next two entities myself, mainly the error caused by "operate911".
I checked the information online and referred to the following article.
Workaround for how to view EntityValidationErrors details
http://www.cnblogs.com/zhourq/archive/2011/11/03/2234720.html
However, when writing the exception, the system does not have "DbEntityValidationException", which is guessed to be a lack of namespace reference.
So added:
[csharp] view plaincopy using System.Data.Validation;
The corresponding procedure is modified to: [csharp] view plaincopy try { entities. Operate911.Add(operate911); entities. SaveChanges(); } catch (DbEntityValidationException dbEx) {
} Start, no exception prompt, "operate911" is not writing data to the database table. The reason for checking is that when writing a supplementary program, there is no data filling for a column that is not allowed to be empty, and after modification, the program is started and the test is successful.
However, if you don't write an exception, continue to prompt the top error.
Moreover, the parameter "dbEx" after the sentence "catch (DbEntityValidationException dbEx)" can be written without writing.
Continue to be unknown...
09.12 Addendum:
The above try and catch are written incorrectly, and the exception is not thrown, but changed to:
[csharp] view plaincopy try { var operateSpt = new OperateSpt(ymd, timeNow, Operate); entities. OperateSpts.Add(operateSpt); entities. SaveChanges(); }
catch (DbEntityValidationException ex) { MessageBox.Show(ex. Message); }
The results are shown in the figure below.
Continue to be unknown...
Supplement: Problem solving.
Reason: The written data is inconsistent with the table settings.
My reason is that at a certain step, the written data is empty, but the original table is set to "not allowed to be empty", so there will be an exception when this step is performed.
I was too careless and never thought of this problem when I saw other operations written to the data when I was looking at the data.
|