查询永不返回

ekk*_*kis 5 c# linq sql-server entity-framework

我有一个简单的LINQ-to-EF查询,由于某种原因,该查询停止了工作,这意味着一旦执行,就永远不会将执行控制返回到下一行。我不知道为什么会这样。如果我使用LINQ-to-SQL在LinqPad中运行相同的查询,则效果很好。这是查询:

Models.Currency curr = _db.Currencies.SingleOrDefault(x => x.ISO == p.Items[i].CurrType);
Run Code Online (Sandbox Code Playgroud)

_db我的实体容器引用在哪里,并且p.Items[i].CurrType包含值“ USD”

可能是什么问题呢?我可以使用哪种诊断方法?

TIA-e!

ps我在Visual Studio 2013和MVC5上运行

*更新我*

按照下面的建议,我在连接字符串(在Web.config中)和命令超时(在* .Context.cs中)中都添加了“连接超时= 10”:

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
        ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 10;
    }
Run Code Online (Sandbox Code Playgroud)

查询仍然挂起(永远不会抛出异常),并且在挂起查询时,我对数据库有一个狂想。我可以看到发出的SQL看起来(或多或少)像:

select * from Currencies
Run Code Online (Sandbox Code Playgroud)

该表应立即返回,因为该表中只有100条小记录。发出查询的连接处于睡眠和等待命令,并且数据库中没有阻塞;有问题的spid执行了0 cpu / io。

我还要看什么?

ekk*_*kis 4

freenode 的 c# 频道的 suchiman 的一点帮助揭示了这个问题:我应该看到异常冒出来,但我没有。他建议我像这样记录数据库输出:

_db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
Run Code Online (Sandbox Code Playgroud)

这揭示了错误:

EntityFramework.dll 中第一次出现“System.NotSupportedException”类型的异常,于 3/17/2015 3:56:43 PM -07:00 关闭连接

他从中巧妙地推断出对p.Items[i].CurrTypelinq 的引用搞乱了。这是他的推理:

[Suchiman] Items[i] is actually a method call
[Suchiman] get_Items(i)
[Suchiman] transformed by the compiler
[Suchiman] when analyzing the expression tree, EF will realize 
[Suchiman] there's a method called get_Items(Int32)
[Suchiman] but it doesn't have any clue how to translate this into SQL
[Suchiman] thats why it throws NotSupportedException
Run Code Online (Sandbox Code Playgroud)

事实证明,如果我像这样重写代码:

var item = p.Items[i];
var curr = _db.Currencies.SingleOrDefault(x => x.ISO == item.CurrType);
Run Code Online (Sandbox Code Playgroud)

有用!