2014-01-16 95 views
4

我正在使用一個查詢,該查詢拉回了大量填充的導​​航屬性。本質上,它看起來像這樣:IO操作中的超時

using(var context = new MyApplicationContext()) 
{ 
    DbSet<BaseTable> dbSet = context.Set<BaseTable>(); 
    IQueryable<BaseTable> query = dbSet; 

    query = query.Include(entity => entity.T.C); 
    query = query.Include(entity => entity.TC.Select(tc => tc.T.M)); 
    query = query.Include(entity => entity.TC); 
    query = query.Include(entity => entity.W.FW.F.S); 
    query = query.Include(entity => entity.W.FW.P); 
    query = query.Include(entity => entity.W.PL.P); 
    query = query.Include(entity => entity.W.PL); 
    query = query.Include(entity => entity.W.E); 
    query = query.Include(entity => entity.E); 

    query = query.Where(set of conditions); 

    List<BaseTable> Loaded = query.ToList(); 
} 

但是,運行該代碼產生在IO操作中的錯誤

超時
[TimeoutException異常:超時在IO操作] MySql.Data.MySqlClient。 TimedStream.StopTimer()+168
MySql.Data.MySqlClient.TimedStream.Read(Byte [] buffer,Int32 offset,Int32 count)+148
System.IO.BufferedStream.Read(Byte [] array,Int 32偏移,的Int32計數)262
MySql.Data.MySqlClient.MySqlStream.ReadFully(流流,字節[]緩衝液,的Int32偏移的Int32計數)86
MySql.Data.MySqlClient.MySqlStream.LoadPacket()+ 110
MySql.Data.MySqlClient.MySqlStream.ReadPacket()59
MySql.Data.MySqlClient.NativeDriver.GetResult(的Int32 & affectedRow,Int64的& insertedId)100
MySql.Data.MySqlClient.Driver.GetResult( Int32 statementId,Int32 & affectedRows,Int64 & insertedId)+54
MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean力)+145
MySql.Data.MySqlClient.MySqlDataReader.NextResult()524
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(的CommandBehavior行爲)1939

我怎樣才能讓這個查詢有有足夠的時間來加載?

回答

9

你需要做的是增加命令超時屬性。

在實體框架中,使用的上下文繼承自DbContext。這是一個IObjectContextAdapter,但不包含對ObjectContext方法的簡單訪問。因此,爲了訪問命令超時屬性,您將需要通過DbContextDatabase屬性。

using(var context = new MyApplicationContext()) 
{ 
    DbSet<BaseTable> dbSet = context.Set<BaseTable>(); 
    IQueryable<BaseTable> query = dbSet; 
//set an increased command timeout by accessing Database property 
    //on the context 
    context.Database.CommandTimeout = 300;//in seconds (5 minutes) 
query = query.Include(entity => entity.T.C); 
    query = query.Include(entity => entity.TC.Select(tc => tc.T.M)); 
    query = query.Include(entity => entity.TC); 
    query = query.Include(entity => entity.W.FW.F.S); 
    query = query.Include(entity => entity.W.FW.P); 
    query = query.Include(entity => entity.W.PL.P); 
    query = query.Include(entity => entity.W.PL); 
    query = query.Include(entity => entity.W.E); 
    query = query.Include(entity => entity.E); 

    query = query.Where(set of conditions); 

    List<BaseTable> Loaded = query.ToList(); 
} 

現在您的查詢可以擁有它,以便在需要時拉開更大的數據量所需要的時間。

+0

你知道它是否足以確定在連接字符串中的默認命令超時?還是必須按照以上所示進行指定? – chriszumberge

+1

@chriszumberge - 這只是爲了顯示代碼中的變量設置。你也可以在連接字符串中設置它,但想法是你可能並不總是想要設置這個長時間的超時。 –

2

使用此代碼在連接字符串在web.config中

default command timeout=0

"server=localhost;Database=Database;uid=;pwd=;Allow User Variables=True;Convert Zero Datetime=True;default command timeout=0"