1

I am using EF core 2.2(Sql server) and trying to handle DbUpdateException. It is a console app done in .Net core.

Exception generated by EF is:

about one of non-nullable field being null during save

To handle this i need to know which entities failed, so that i can log them and detach them from context as same context is used to save other records in loop setup but for my case; entries list is always empty when i get error.

Please refer image down:

entries list is empty for failed entity save/update

I am following this link as this looked having good suggestions around handling same:

handle DbUpdateException

Can anyone please share any documentation/directions/links around what all possible reasons are there behind having entries list 0 so that i can match/fix mine and get entries list loaded.

FYI

I am having solution designed in EF core 2.2 with generic repository and unit of work pattern if that matters in any ways. But i tried handling exception in all layers, unit of work, repository as well as overridden savechanges in context and all the places it comes empty.

Microsoft documentation says this, but i don't find it detailed enough to fix for my scenario.

Typically this is a single entry, but in some cases it may be zero or multiple entries

LearningNeverEnds
  • 374
  • 1
  • 3
  • 22
  • What is the specific DbUpdateException that is caught there? Some exceptions (for me there was a "deadlock detected" exception) have no entries and there is no handling for specific entities available. – DevilSuichiro Feb 08 '19 at 10:36
  • @DevilSuichiro Thanks for quick response. In my case its is throwing error about one of non-nullable field being null during save... which i expected to be present in entries hence didn't mention cause of exception in post, but will edit to convey this as well. – LearningNeverEnds Feb 08 '19 at 10:39
  • if a not-nullable field is passed as null by EF, that typically means that the schema does not match the model. Probably you should add a validation step, if that was intended. Nevertheless, I too would have expected an entity in the exception entries. – DevilSuichiro Feb 08 '19 at 10:53
  • yes that was my next thought that i shall also add my own validation in my savechanges in context, then only it may store entries in exception. it seems much needed per this link(its bit old so can't be sure) but haven't tried it yet: https://stackoverflow.com/questions/46430619/net-core-2-ef-core-error-handling-save-changes – LearningNeverEnds Feb 08 '19 at 14:56
  • on adding a validation step: i wanted a generic dbupdated exception handler and logging mechanism, the null being passed was one scenario, but there was another scenario where data in a field was large enough to cause exception, hence apart from validations to be added, i still want entries to be available for fallback for missing validations in future when code is maintained. Thanks – LearningNeverEnds Feb 08 '19 at 14:59

0 Answers0