Skip to content

Commit a4bb3ae

Browse files
authored
Update README.md
1 parent afb0979 commit a4bb3ae

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,20 @@ EfCore.BulkOperations utilizes local transactions within bulk processes. If you
9191

9292

9393
```csharp
94-
var connection = _dbContext.Database.GetDbConnection();
95-
DbTransaction? transaction = null;
94+
IDbContextTransaction? transaction = null;
95+
DbConnection? connection = null;
9696
try
9797
{
98-
transaction = await connection.BeginTransactionAsync();
98+
connection = _dbContext.Database.GetDbConnection();
99+
if (connection.State != ConnectionState.Open) await connection.OpenAsync();
100+
transaction = await _dbContext.Database.BeginTransactionAsync();
101+
var dbTransaction = transaction.GetDbTransaction();
102+
99103
var insertItems = new List<Product> { new Product("Product1", 100m) };
100-
await _dbContext.BulkInsertAsync(insertItems, null, transaction);
104+
await _dbContext.BulkInsertAsync(insertItems, null, dbTransaction);
101105
var updateItems = new List<Product> { new Product("Product2", 200m) };
102-
await _dbContext.BulkUpdateAsync(updateItems, null, transaction);
106+
await _dbContext.BulkUpdateAsync(updateItems, null, dbTransaction);
107+
103108
await transaction.CommitAsync();
104109
}
105110
catch (Exception e)
@@ -108,7 +113,7 @@ catch (Exception e)
108113
}
109114
finally
110115
{
111-
if(transaction is not null) await transaction.DisposeAsync();
112-
await connection.CloseAsync();
116+
if (connection is { State: ConnectionState.Open }) await connection.CloseAsync();
117+
if (transaction != null) await transaction.DisposeAsync();
113118
}
114119
```

0 commit comments

Comments
 (0)