IO操作超时

时间:2014-01-16 19:31:41

标签: c# mysql entity-framework

我正在使用一个查询,该查询撤回了大量已填充的导航属性。基本上它看起来像这样:

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 8      MySql.Data.MySqlClient.TimedStream.Read(Byte []缓冲区,Int32偏移量,Int32计数)+148
     System.IO.BufferedStream.Read(Byte []数组,Int32偏移量,Int32计数)+262
     MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream,Byte [] buffer,Int32 offset,Int32 count)+86
     MySql.Data.MySqlClient.MySqlStream.LoadPacket()+110
     MySql.Data.MySqlClient.MySqlStream.ReadPacket()+59
     MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow,Int64&amp; insertedId)+100
     MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId,Int32&amp; affectedRows,Int64&amp; insertedId)+54
     MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId,Boolean force)+145
     MySql.Data.MySqlClient.MySqlDataReader.NextResult()+524
     MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)+1939

如何让此查询有足够的时间加载?

2 个答案:

答案 0 :(得分:11)

您需要做的是增加命令超时属性。

在实体框架中,使用的上下文继承自DbContext。这是IObjectContextAdapter,但不包含对ObjectContext方法的轻松访问权限。因此,为了访问命令超时属性,您需要浏览DbContext的{​​{3}}属性。

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();
}

现在,您的查询可以拥有所需的时间,以便在必要时提取更多数据。

答案 1 :(得分: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"