Suppose the code has the following structure:

using (var tran = new TransactionScope()) { int i = 0; foreach (var val in col) { i++; if (i == 10) { i = 0; tran.Complete(); } //Какие-то действия с бд } } 

On msdn it says that Complete can be called once. Is it possible to come up with something that, using 1 TransactionScope perform several commits.

In other words, I want every 10 iterations of the loop to be like 1 transaction.

    1 answer 1

    If you read MSDN carefully, you will see that the actual commit (or rollback) of the transaction does not occur in the Complete() method, but when you call Dispose() - that is, at the end of the using block. Therefore, no matter how you twist, and the only way out is to create your own skoup for every 10 iterations.

    To simplify the code a little bit, you can use the Batch () method (another implementation variant is here ), and process each packet in your own script. It will look something like this:

     foreach (var batch in col.Batch(10)) { using (var tran = new TransactionScope()) { foreach (var val in batch) { //Какие-то действия с бд } tran.Complete(); } }