在实体框架中使用选择锁定表 [英] Locking a table with a select in Entity Framework
问题描述
我需要做这样的事情
select * from myTable with (xlock,holdlock)
使用实体框架.这可能吗?我已经打开了Serializable
隔离级别的TransactionScope
,但是我的选择没有锁定表.我希望他们锁定,直到完成交易范围.
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.
推荐答案
可以,但是您必须发出使用LINQ时无法添加锁定提示的SQL(据我所知):
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/
这篇关于在实体框架中使用选择锁定表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!