Linq to Entities 超时问题

4

我有以下的linq查询,当foreach循环枚举数据时超时:

public IQueryable<Client> GetTopLevelData(Guid agentGuid, int year)
    {
        var clients = from client in ObjectContext.Clients
                join cbc in ObjectContext.Client_Bucket_Client on client.Client_GUID equals cbc.Client_GUID
                join acb in ObjectContext.Agent_Client_Bucket on cbc.Client_Bucket_GUID equals acb.Client_Bucket_GUID
                where acb.Agent_GUID == agentGuid
                orderby client.Last_Change_Date descending, client.File_Under 
                select client;

        var clientInfos =
            from c in clients
            select new
            {
                Client = c,
                TransactionInfos = ObjectContext.Transactions
                    .Where(t => t.Client_GUID == c.Client_GUID && t.Year == year)
                    .Select(t => new
                    {
                        Transaction = t,
                        ToAttach = ObjectContext.Forms.Where(f => f.Transaction_GUID == t.Transaction_GUID && f.Year == year) //.OrderByDescending(fo => fo.Create_Date);
                    })
            };

        // Looping over this query will hit the database *once*
        foreach (var info in clientInfos)
        {
            foreach (var transactionInfo in info.TransactionInfos)
            {
                transactionInfo.Transaction.Forms.Attach(transactionInfo.ToAttach);
            }

            var tt = info.TransactionInfos.ToList(); //.Select(t => t.Transaction);

            var trans = tt.Select(t => t.Transaction);

            info.Client.Transactions.Attach(trans);
        }
1个回答

2

您可以在连接字符串中设置超时时间。

"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer;Connect Timeout=30";

连接超时是以秒为单位计算的。


谢谢Kim!我添加了连接超时并将其设置为120,它起作用了!似乎在我的系统上,默认值为15秒。这是在连接字符串中没有连接超时参数时通常运行的时间,然后抛出异常。 - Ruggeri
@Ruggeri 没问题 :) 所有系统默认为15秒。 - Kimtho6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接