Search code examples
c#oracle-databaseentity-frameworkdapper-plus

DapperPlus and Oracle, know which row is throwing an error when inserting to DB


We are currently facing an issue with our code, which is used to push thousands (sometimes more) of entries in our database at a time.

The technologies we are using for this are :

  • C#
  • Entity Framework (latest version)
  • Oracle 12c
  • Dapper Plus (latest version)

Our code looks a bit like this :

Try
{
    Trans.BulkInsert(ParentEntity)
         .AlsoBulkInsert(c => c.ChildEntityA)
         .ThenBulkInsert(c => c.ChildEntityB);
}catch(Exception ex)
{
    *error management*
}

For most of our use cases, this works perfectly fine. But when an error occurs during the insertion in the DB, we are left with a bit of an issue : We are currently unable to tell which row specifically triggered the error, forcing us to set the whole batch of data in Error.

The Exception we receive is of type Oracle.ManagedDataAccess.Client.OracleException which, unlike a DbUpdateException does not posses the ".Entries" property.

For legacy reasons, we are also unable to access the context directly, we have to use a factory that gives us a very limited access (we can basically obtain a connection string, and initialize a connection through the factory, but not much more).

Is there any way, with the technologies we are using, and the restrictions we are facing, to know which row caused an error at insertion ?


Solution

  • The problem when bulk inserting is that we never know exactly which row is in error. We only know the insert failed.

    It's possible to retry one by one when an error occurs and get information about the error:

    var errors = new List<BulkOperationError>();
    
    connection.UseBulkOptions(options =>
    {
                    options.ErrorMode = ErrorModeType.RetrySingleAndContinue;
                    options.Errors = errors;
    }).BulkInsert(list);
    

    However, be careful, this feature has only been tested with SQL Server.

    If you have an issue, I recommend you to contact us directly or post it on GitHub: https://github.com/zzzprojects/Dapper-Plus/issues