Questions tagged [transactionscope]

TransactionScope is a .NET class used to mark a block of code as transactional. It uses an implicit programming model so transactions are managed by the infrastructure, rather than the developer. The class was introduced in .NET 2.0.

Description

The TransactionScope class is part of the System.Transactions namespace. Unlike the System.Transactions.Transaction class, which uses an explicit programming model, the TransactionScope class uses an implicit programming model, which simplifies how transactions are used in client code.

How to use it

To use TransactionScope, the developer simply creates a scope, does the transactional work and then indicates when the transaction is complete. There is no need to explicitly rollback the transaction in the case of an error.

 using(var scope = new TransactionScope())
 {
      // do database or other transactional work here
      scope.Complete();
 }

Inside the scope, resources are automatically enlisted in the ambient transaction so there is no need to manage the transaction directly. The transaction is automatically rolled back unless it is marked as complete before TransactionScope is disposed.

References

TransactionScope Class (MSDN)

1049 questions
38
votes
7 answers

How to use TransactionScope in C#?

I am trying to use TransactionScope, but keep getting the exception below. The app is running on a different machine than the database, if that matters. I am using SQL Server 2005. Network access for Distributed Transaction Manager (MSDTC) has…
NotDan
  • 31,709
  • 36
  • 116
  • 156
38
votes
3 answers

Is it possible to use System.Transactions.TransactionScope with SqlBulkCopy?

Very simple question: is it possible to use System.Transactions.TransactionScope together with SqlBulkCopy? The documentation Transaction and Bulk Copy Operations doesn't mention anything (at least as of .NET 4.0) and my testing indicates it does…
jason
  • 236,483
  • 35
  • 423
  • 525
36
votes
5 answers

Multiple SaveChanges calls in entity framework

I am building my own custom repository, based on entity framework, and I'm creating some extension methods that allow me to save partial view models as entity models so I'm building my own Add and Update methods. Currently, each method has…
Admir Tuzović
  • 10,997
  • 7
  • 35
  • 71
34
votes
3 answers

How to know if the code is inside TransactionScope?

What is the best way to know if the code block is inside TransactionScope? Is Transaction.Current a realiable way to do it or there are any subtleties? Is it possible to access internal ContextData.CurrentData.CurrentScope (in System.Transactions)…
nightcoder
  • 13,149
  • 16
  • 64
  • 72
34
votes
6 answers

NHibernate with TransactionScope

Can anyone give me a quick overview of using TransactionScope with NHibernate? Do I need to do anything special with the session/IEnlistmentNotification/etc. to get this to work? Are there any pitfalls that I should worry about? For example, can I…
Andy White
  • 86,444
  • 48
  • 176
  • 211
34
votes
1 answer

How to use TransactionScope properly?

I always want to try to use TransactionScope but I just can't figure out what people see about it that is useful. So let's take an example: using(TransactionScope tran = new TransactionScope()) { CallAMethodThatDoesSomeWork1(); …
Denis
  • 11,796
  • 16
  • 88
  • 150
33
votes
6 answers

Refactoring ADO.NET - SqlTransaction vs. TransactionScope

I have "inherited" a little C# method that creates an ADO.NET SqlCommand object and loops over a list of items to be saved to the database (SQL Server 2005). Right now, the traditional SqlConnection/SqlCommand approach is used, and to make sure…
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
32
votes
3 answers

How does TransactionScope work?

When Method1() instantiates a TransactionScope and calls Method2() that also instantiates a TransactionScope, how does .NET know both are in the same scope? I believe it doesn't use static methods internally otherwise it wouldn't work well on…
Eduardo
  • 5,645
  • 4
  • 49
  • 57
31
votes
3 answers

Ignore TransactionScope for specific query

I'm looking for a way to execute a query while a TransactionScope is alive, and ignore the TransactionScope - basically, I want to execute this particular query no matter what. I'm using EF code-first, and the way the application is designed, a new…
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
30
votes
3 answers

How to support async methods in a TransactionScope with Microsoft.Bcl.Async in .NET 4.0?

I have a method similar to: public async Task SaveItemsAsync(IEnumerable items) { using (var ts = new TransactionScope()) { foreach (var item in items) { await _repository.SaveItemAsync(item); } …
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
29
votes
1 answer

Nested/Child TransactionScope Rollback

I am trying to nest TransactionScopes (.net 4.0) as you would nest Transactions in SQL Server, however it looks like they operate differently. I want my child transactions to be able to rollback if they fail, but allow the parent transaction to…
Robert Wagner
  • 17,515
  • 9
  • 56
  • 72
29
votes
2 answers

C# controlling a transaction across multiple databases

Say I'm having a Windows Form application which connected to n databases, with n connections opened simultaneously. What I'm looking for is to do a transaction with all of those databases in one go. For example if I were to have 2 database…
Samuel Adam
  • 1,327
  • 4
  • 26
  • 45
27
votes
8 answers

Why doesn't TransactionScope work with Entity Framework?

See the code below. If I initialize more than one entity context, then I get the following exception on the 2nd set of code only. If I comment out the second set it works. {"The underlying provider failed on Open."} Inner: {"Communication with…
NotDan
  • 31,709
  • 36
  • 116
  • 156
25
votes
3 answers

Using TransactionScope with Entity Framework 6

What I can't understand is if its possible to make changes to the context and get the changes in the same transaction before its commited. This is what I´m looking for: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { …
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
25
votes
4 answers

One transaction with multiple dbcontexts

I am using transactions in my unit tests to roll back changes. The unit test uses a dbcontext, and the service i'm testing uses his own. Both of them are wrapped in one transaction, and one dbcontext is in the block of the other. The thing is, when…
Mark Homans
  • 637
  • 1
  • 6
  • 12
1
2
3
69 70