Entity Framework locking a table with a select

entity-framework table-locking transactionscope

Question

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.

1
8
10/28/2013 7:09:13 AM

Popular Answer

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/

6
7/18/2010 9:35:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow