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

Cannot rollback after BulkSaveChanges exception

Open ibootbg opened this issue 5 years ago • 3 comments

Description

I'm trying to rollback transaction in case BulkSaveChanges() throws exception. However transaction.Rollback() throws an exception: "The underlying provider failed on Rollback." If I use ctx.SaveChanges() instead of ctx.BulkSaveChanges() the code works as expected.

class Contact
{
    public int ContactId { get; set; }
    // no MaxLength attribute - we want the db to fail if length exceeded
    // db field is varchar(50)
    public string Address { get; set; }
}

var tran = ctx.Database.BeginTransaction();
try
{
    foreach (var c in ctx.Contacts.ToList())
    {
        // deliberately assign too long address to cause exception
        c.Address = "AddressMax10AddressMax20AddressMax30AddressMax40AddressMax50";
    }

    ctx.BulkSaveChanges(false);
    tran.Commit();
}
catch (Exception e)
{
    var t = ctx.Database.CurrentTransaction;
    // t is null, so Rollback fails
    tran.Rollback();
    throw;
}

Exception

Exception message: System.Data.Entity.Core.EntityException: The underlying provider failed on Rollback. ---> System.ArgumentNullException: Value cannot be null. Parameter name: connection

Stack trace: at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName) at System.Data.Entity.Infrastructure.Interception.DbTransactionInterceptionContext.WithConnection(DbConnection connection) at System.Data.Entity.Infrastructure.Interception.DbTransactionDispatcher.Rollback(DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Core.EntityClient.EntityTransaction.Rollback() --- End of inner exception stack trace --- at System.Data.Entity.Core.EntityClient.EntityTransaction.Rollback() at System.Data.Entity.DbContextTransaction.Rollback() at NewHqQueryTest.Program.TestBulkInsertRollback() in ... at NewHqQueryTest.Program.Main() in ...

Further technical details

  • EF version: 6.2.0
  • EF Extensions version: 4.0.69
  • Database Provider: SQL Server

ibootbg avatar May 07 '20 14:05 ibootbg

Thank you for reporting, we will look at it.

JonathanMagnan avatar May 07 '20 15:05 JonathanMagnan

Hello @ibootbg ,

My developer made an online example which looks exactly like your case: https://dotnetfiddle.net/TLKodd

However, as you will see, everything works as expected.

We probably miss a little piece of the puzzle to reproduce it.

Could you try to reproduce it on your side in a standalone project or directly in Fiddle?

That will help us to fix this issue faster.

Best Regards,

Jon

JonathanMagnan avatar May 08 '20 00:05 JonathanMagnan

You can find sample project here. Please use the provided SQL script to create and populate the SQL table. Also note that you must not use MaxLength atribute.

ibootbg avatar May 08 '20 16:05 ibootbg