Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

transactionscope - Locking a table with a select in Entity Framework

I need to do something like this

select * from myTable with (xlock,holdlock)

using Entity Framework. Is this possible? I've opened a TransactionScope with the Serializable isolation level but my selects are not locking the tables. I'd like them to lock until I complete the transaction scope.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is possible but you have to issue the SQL you can't add the locking hint when using LINQ (as far as I know):

ObjectContext.ExecuteStoreCommand(
                string.Format("select 1 from [{0}] with (tablockx, holdlock) where 0 = 1",
                              tableName));

If you do that in a transaction scope then you'll hold the lock until you complete the transaction.

A bit more information can be found here:

http://peplowdown.wordpress.com/2010/07/18/locking-across-servers-table-locks-with-entity-framework/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...