EntityFramework-Extensions
EntityFramework-Extensions copied to clipboard
BulkSaveChanges throws too general exception upon error
Description
BulkSaveChanges throws too general exception upon error: Exception.
E.g. when an SqlException occurs then this is happening:
| Operation | Explanation | Exception structure |
|---|---|---|
SaveChanges |
wraps it twice | DbUpdateException(UpdateException(SqlException)) |
BatchSaveChanges |
wraps it once (see #280) | UpdateException(SqlException) |
BulkSaveChanges |
wraps it once | Exception(SqlException) |
I think BulkSaveChanges should at least wrap it into an UpdateException instead of Exception (i.e. UpdateException(SqlException)), or even better: wrap it twice just like the original SaveChanges does (i.e. DbUpdateException(UpdateException(SqlException))).
Fiddle
https://dotnetfiddle.net/Sd3V3u
Further technical details
- EF version: 6.2.0
- EF Extensions version: 3.22.5
- Database Provider: SQL Server
Thank you @davidnemeti for reporting,
We started to look at the BatchSaveChanges exception wrapping. We will make this method first and check after for BulkSaveChanges.
Best Regards,
Jonathan
Hello @davidnemeti ,
The v3.22.7 has been released.
The exception should now be wrapped in a DbUpdateException for BatchSaveChanges.
Let me know if everything now works as expected for this method.
Any updates on this issue? We are using EF Core so BatchSaveChanges is not an option.
My problem is that when I do an insert of many items and then do BulkSaveChanges (or BulkInsert - same issue there) I want to be able to retry the insert with the failed entities removed (DbUpdateException contains a Entries property that has this info).
If I use the normal SaveChanges everything works fine and I can remove the bad entities and retry the insert.
(by the way - let me know if you want me to open a new issue for this. Happy to do that)
Although BatchSaveChanges now behaves properly, BulkSaveChanges still throws an Exception(SqlException). The current situation for Z.EntityFramework.Extensions at version 7.100.0.5 is this:
| Operation | Explanation | Exception structure |
|---|---|---|
✔️ SaveChanges |
wraps it twice | DbUpdateException(UpdateException(SqlException)) |
✔️ BatchSaveChanges |
wraps it twice | DbUpdateException(UpdateException(SqlException)) |
❌ BulkSaveChanges |
wraps it once | Exception(SqlException) |
Ok, thanks for the update.
I've done some further testing on my end and it seems the standard EF Core SaveChanges (v7.0.13) also doesn't do the right thing for me. While it does wrap the exception in a DbUpdateException, the list of Entries always contains all entities that participated in the insert, not only the ones that failed.
I've resorted to a fallback of "insert one by one" instead which works for now. It would be nice if BulkInsert or BulkSaveChanges could do this though - I've logged issue #555 to track this enhancement request