Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Sql Server中执行查询的死锁_C#_Sql Server_Linq_Deadlock - Fatal编程技术网

C# Sql Server中执行查询的死锁

C# Sql Server中执行查询的死锁,c#,sql-server,linq,deadlock,C#,Sql Server,Linq,Deadlock,我有以下Linq: var qry=s.GetTable()。其中(x=>x.MessageName==MessageName&&x.SourceTyp==SourceTyp&&x.Source==Source&&x.Status==MessageEventStatus.open | | x.Status==MessageEventStatus.conferenced)); goneMessages=qry.ToList(); var-ret=qry .Set(x=>x.Status,x=>x.

我有以下Linq:

var qry=s.GetTable()。其中(x=>x.MessageName==MessageName&&x.SourceTyp==SourceTyp&&x.Source==Source&&x.Status==MessageEventStatus.open | | x.Status==MessageEventStatus.conferenced));
goneMessages=qry.ToList();
var-ret=qry
.Set(x=>x.Status,x=>x.Status | MessageEventStatus.gone)
.Set(x=>x.timeStamp,timeStamp)
.Update();
返回ret;
将转换为以下SQL:

updatemessageevents SET Status=Status | 1,TimeStampGone=@1
其中MessageName=@2和SourceTyp=@3 Source=@4和(Status=0或Status=2)
现在的问题是,有多个更新并行运行,我得到了死锁异常,但我不明白为什么

另见


如果您不希望其他进程能够启动更新的选择部分,请使用
UPDLOCK
提示或设置适当的事务隔离级别(
可重复读取

有关非聚集索引如何导致死锁的更全面解释,请参阅

使用Linq to SQL的示例不受此问题的影响:

var opts = new TransactionOptions();
opts.IsolationLevel = IsolationLevel.RepeatableRead;
using (var txn = new TransactionScope(TransactionScopeOption.Required, opts))
{
    // update command goes here.

    txn.Complete();
}

表上有什么索引?索引优化通常可以避免死锁。您是否在代码中打开事务?我认为不使用事务时不应该发生这种情况。。。