Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

How to get a SqlTransaction object from a EF Core 5 DBContext transaction object as I need to pass it to SqlBulkCopy operation

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...