I'm trying to start a transaction on DbContext and doing some operation on context and then wants to pass the same transaction to SqlBulkCopy operation as 3rd parameter.
In EF Core 5, DbContent class I did not find a way to get a SqlTransaction/DbTransaction object which can be passed to SqlBulkCopy operation. In code _contextTransaction object is of type DbContextTransaction and It seems it is a breaking change from MS in latest EF Core. I have added [not compatible] in the code for easy understanding.
If someone can suggest a workaround/solution.
using (var _context = new DBContext())
{
using (var _contextTransaction = _context.Database.BeginTransaction())
{
_context.SomeEntity.Add(obj);
_context.SaveChanges();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(_context.Database.GetDbConnection() as SqlConnection, SqlBulkCopyOptions.Default, _context.Database.CurrentTransaction [not compatible]))
{
bulkCopy.DestinationTableName = "SomeTable";
bulkCopy.BulkCopyTimeout = 10000;
bulkCopy.BatchSize = 10000;
...........
await bulkCopy.WriteToServerAsync(partition);
}
}
}
question from:
https://stackoverflow.com/questions/65850947/how-to-get-a-sqltransaction-object-from-a-ef-core-5-dbcontext-transaction-object 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…