tags:

views:

298

answers:

1

we can use sql just like this:select * From Student with(nolock)

how can i impl this in Linq to sql?

Note:I don't want to use TransactionScope

expect your answer

+2  A: 

LINQ to SQL does not have any mechanism of doing this, but you can create a transaction with a specific isolation level. Look at the code below:

using (var con = new SqlConnection("constr"))
{
    con.Open();
    using (var tran = con.BeginTransaction(IsolationLevel.ReadUncommitted))
    {
        using (var context = new SchoolDataContext(con))
        {
            context.Transaction = tran;
            var q = from s in context.Students select c;
        }
    }
}

Sometimes using this type of isolation is useful, i.e. for performance reasons. But please make sure you don't do any create, update or delete (CUD) operations using this type of database isolation. It of course depends on your situations, but your data could get in an inconsistent state.

Steven
forgive my late response, it's Chinese Spring Festival recently.The reason is partly historical:the member of my team always write nolock statement in simple three-layer architecture. they don't want to lose the control of DataBase. they even want to use nolock in Nhibernate , i give up Nhibernate just for this.They don't fully understand ORM's job.Now,my question become how to persuade them use ORM without considering details about Data Access????
ligaoren
P.S Steven's answer is brilliant
ligaoren
Thanks. Don't forget to check it as answer and vote it up. I like the credits :-)
Steven