Find out the exact entity that throws an exception in the entity structure

The entity structure gives me general messages in the exception without telling me the exact entity and attribute that caused the error. How to get additional error information?

This happens in many cases, such as

The operation failed: the relation cannot be changed because one or more properties of the foreign key are not NULL. When a change in relationship occurs, the corresponding property of the foreign key is set to zero. If the foreign key does not support null values, a new relationship must be defined, another nonzero value must be assigned to the foreign key property, or an object not associated with it must be deleted.

and

Converting the datetime2 data type to the datetime data type has exceeded the value out of range. Application completed.

Exception Details:

[SqlException (0x80131904): Converting a datetime2 data type to a datetime data type results in a value out of range. The application has been terminated.] System.Data.SqlClient.SqlConnection.OnError (SqlException exception, logical connection disconnection) +404 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning () +412 System.Data.SqlClient.TdsParser.Run (RunBehavior , SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2660 System.Data.SqlClient.SqlDataReader.ConsumeMetaData.Data.Sqlget.Data.SqlgetDlate.LetDlientItetDlateItget.DlateIlgetItelDaIlReItLaItReIlDlient (SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +6431425 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds (CommandBehavior cmdBehavior, RunBehavior runBehavior CommandBehbeclnhdjehmenjecln1e1cd1n1emecd1n1e1cd1n1emecd1n1e1e1e1e1n4aaaaaaaaaaaaaaaaaaaejeanjejejehdjejehdjaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa runBehavior, B oolean returnStream, String method, result of DbAsyncResult) +538 System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +28 System.Data.SqlClient.SqlCommizard.method ) +256 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader (CommandBehavior behavior) +19 System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute (UpdateTranslator translator, EntityConnection connection, dictionary 2 identifierValues, List 1 generatedValues.) Data.Mapping.Update.Internal.UpdateTranslator.Update (IEntityStateManager stateManager, IEntityAdapter adapter) +391

[UpdateException: error updating records. See the Internal Exception for more details.] System.Data.Mapping.Update.Internal.UpdateTranslator.Update (IEntityStateManager stateManager, adapter IEntityAdapter) +11223976 Parameters System.Data.Objects.ObjectContext.SaveChanges (SaveOptions) +833 System.Data.Entity. Internal.InternalContext.SaveChanges () +218

[DbUpdateException: An error occurred while updating records. See Internal Exception for more details.] System.Data.Entity.Internal.InternalContext.SaveChanges () +291

+6
source share
2 answers

Here is the code I have in my solution:

 try { _context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); //raise a new exception inserting the current one as the InnerException raise = new InvalidOperationException(message , raise); } } throw raise; } 

You can use it as a basis for adding to your solution ... it creates a nested exception set with all the details from the Entity Framework.

+7
source

You need to write tests for repositories and a base class for your tests:

 try { DbContext.SaveChanges(); } catch (DbEntityValidationException e) { e.EntityValidationErrors.SelectMany(error => error.ValidationErrors).ToList().ForEach( item => Console.WriteLine("{0} - {1}", item.PropertyName, item.ErrorMessage)); throw; } 
+1
source

Source: https://habr.com/ru/post/944159/


All Articles