Home >Backend Development >C++ >How to Debug 'Validation Failed During Database Initialization' Errors in Entity Framework?
Database initialization During verification failure
This error usually occurs when the data is inserted during the database sowing process, and the input data does not meet the definition of verification rules. In this example, eight verification errors were encountered during the sowing process.
The error message prompts you to check the "Entity ValidationError" property to obtain more details. To visit these errors, you can follow the steps below:
Context.savechanges () in the seed method calls the try-catch block around the surrounding method.
Access verification error
To view the actual verification error in the CATCH block, you can use the following code:
This code will output the detailed information of each verification error, including the physical type, attribute name, and error message.
<code class="language-c#">try { //您的代码... context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("类型为 \"{0}\" 的实体,状态为 \"{1}\",具有以下验证错误:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- 属性:\"{0}\",错误:\"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; }</code>Verify error example
One possible reason for the error is the lack or invalid state value in the Applicantposition table. Before trying to save the database changes, make sure the Statusid property is set to an effective state ID.
The above is the detailed content of How to Debug 'Validation Failed During Database Initialization' Errors in Entity Framework?. For more information, please follow other related articles on the PHP Chinese website!