How to use WITH (NOLOCK) in LINQ to SQL?

we can use SQL like this:

SELECT * FROM student WITH(NOLOCK); 

How can I achieve this with LINQ to SQL without using TransactionScope ?

+2
sql linq-to-sql transactions
Feb 08 '10 at 10:09
source share
1 answer

LINQ to SQL has no mechanism for this, but you can create a transaction with a certain level of isolation. Take a look at the code below:

 using (var con = new SqlConnection("constr")) { con.Open(); using (var transaction = con.BeginTransaction( IsolationLevel.ReadUncommitted)) { using (var context = new SchoolDataContext(con)) { // HACK: Setting the context.Transaction is // needed in .NET 3.5 (fixed in .NET 4.0). context.Transaction = transaction; var q = from s in context.Students select c; } } } 

Using this type of insulation is sometimes useful, that is, for performance reasons. But please do not do any create, update, or delete (CUD) operations using this type of database isolation. This, of course, depends on your situations, but your data may be in an inconsistent state.

+9
Feb 08 '10 at 10:38
source share



All Articles