Locking a table with a select in Entity Framework

时间秒杀一切 提交于 2019-12-18 07:45:08

问题


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:


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/



来源:https://stackoverflow.com/questions/1625784/locking-a-table-with-a-select-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!