This project has moved. For the latest updates, please go here.

Why transaction scope and not EF tx?

Apr 8, 2016 at 1:14 PM
Documentation says (along with an example):
NB! This action is executed immediately in its own transaction. To combine bulk insert with DbContext, TransactionScope must be used..
Is there a reason why TransactionScope must be used instead of a transaction from EF ( ctx.BulkInsert(entities, ctx.Database.CurrentTransaction.UnderlyingTransaction))?

I have tried it and AFAICT it works fine for commit and rollback (EF 6.1.3):
using (var ctx = new DataContext())
{
    var carCount = ctx.Cars.Count();
    using (var tx = ctx.Database.BeginTransaction())
    {
        ctx.Cars.Add(new Car { Color = "Unicorn white" });
        ctx.SaveChanges();

        var bulkCars = new[] { 
                                new Car { Color = "Balrog red" }, 
                                new Car { Color = "Octarina" } 
        };
        ctx.BulkInsert(bulkCars, tx.UnderlyingTransaction);
        // or ctx.BulkInsert(bulkCars, 
        //         ctx.Database.CurrentTransaction.UnderlyingTransaction);

        tx.Rollback();
    }

    var newCarCount = ctx.Cars.Count();
    Assert.AreEqual(carCount, newCarCount);
}