Can I prevent a LINQ DataContext from using a transaction when a TransactionScope is present?

StackOverflow https://stackoverflow.com/questions/17819103

  •  04-06-2022
  •  | 
  •  

Question

I'm using a TransactionScope in a model controller class that coordinates several lower level, data access classes. The data access classes each use their own LINQ DataContext, and thanks to the magic of TransactionScope, they all participate in the same transaction if one is present.

Under normal circumstances, this is perfect and everything works. However, I've added an activity logging class and one of the places it can write to is the database. Unfortunately, it automatically picks up on the TransactionScope and if the transaction gets rolled back, so do all the log entries.

I've checked the Transaction property of the DataContext and it's null, as expected, so I'm not sure how to tell it to ignore the TransactionScope.

Was it helpful?

Solution

In your logging class, wrap your using(new datacontext()) into:

using (var s = new TransactionScope(TransactionScopeOption.Suppress)) {

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top