Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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
Sql server EF阻止对数据库表的访问_Sql Server_Entity Framework - Fatal编程技术网

Sql server EF阻止对数据库表的访问

Sql server EF阻止对数据库表的访问,sql-server,entity-framework,Sql Server,Entity Framework,我有CodeFirstweb应用MVC4+EF+SQLServer。有Statistic表,其中包含DateStamp(DateTime)列。web应用程序在Statistic表中对每个这样的请求执行计算(Where和Count) public IQueryable<Statistic> GetStatisticForCurrentMonthByIp(string ip) { return _context.Statistic.Where(p => p.Ip == ip

我有
CodeFirst
web应用
MVC4+EF+SQLServer
。有
Statistic
表,其中包含
DateStamp(DateTime)
列。web应用程序在
Statistic
表中对每个这样的请求执行计算(Where和Count)

public IQueryable<Statistic> GetStatisticForCurrentMonthByIp(string ip)
{
   return _context.Statistic.Where(p => p.Ip == ip && SqlFunctions.DateDiff("mm", DateTime.UtcNow, p.DateStamp) == 0);
} 
问题是,有时我需要删除
Statistic
表中的所有记录,但web app会锁定
Statistic
表。当我尝试在SQLServer中执行SQL时,它只是冻结

DELETE FROM Statistic
你知道怎么解决这个问题吗

它只是冻结了

因此,您需要调查SQL Server中的阻塞。一个简单的工具是。这有点过时,但仍然有用:

您将发现读块写和写块读很可能是因为您的隔离级别(请参阅)

使用批量删除在生产中是非常有问题的,并且总是造成阻塞。一个更好的选择是,如果你真的想要核武器的内容。应该在小批量中使用DELETE,这不仅是为了防止彻底的阻塞,而且您也不会因为容纳每一行删除所需的增长而导致大量日志中断

你知道怎么解决这个问题吗


调查。确保不使用
newtransactionscope()

此函数是如何调用的?由于您返回的是IQuerably,因此您不会仅使用此方法执行任何实际查询。代码已更新。我用ToList()调用该方法来执行query.TRUNCATE,成功了!也感谢您阅读推荐。
DELETE FROM Statistic