EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

BulkSaveChanges throws too general exception upon error

Open davidnemeti opened this issue 6 years ago • 5 comments

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

davidnemeti avatar Sep 10 '19 12:09 davidnemeti

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

JonathanMagnan avatar Sep 10 '19 13:09 JonathanMagnan

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.

JonathanMagnan avatar Sep 11 '19 15:09 JonathanMagnan

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)

isak1080 avatar Nov 02 '23 13:11 isak1080

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)

davidnemeti avatar Nov 02 '23 15:11 davidnemeti

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

isak1080 avatar Nov 03 '23 09:11 isak1080