.toList() return 异常

.toList() return an exception

我有这个方法 运行 针对 MySQL 数据库,结果是下面所有代码的异常。

然后尝试使用 take(10) 方法进行查询,它工作正常,但仍无法弄清楚原因。

我正在迭代的 table 有数百万行。 有人可以解释发生了什么以及为什么吗? 谢谢

public IHttpActionResult retentionRate() {   
        DateTime today = DateTime.Today;
        DateTime firstOfThisMonth = new DateTime(today.Year, today.Month, 1);
        DateTime  LastYearDate = DateTime.Today.AddDays(-365);

            var activeInDateRange = (from mp in FitnessDbo.memberproductinfoes
                                     where mp.mepi_expirydate >= LastYearDate &&
                                     mp.mepi_activationdate < today
                                     select new
                                     {
                                         mepi_memberproductinfoid = mp.mepi_memberproductinfoid,
                                         mepi_memberid = mp.mepi_memberid,
                                         mepi_activationdate = (mp.mepi_activationdate < LastYearDate ? LastYearDate : mp.mepi_activationdate),
                                         mepi_expirydate = (mp.mepi_expirydate > today ? today : mp.mepi_expirydate),
                                         mepi_prodtype = mp.mepi_prodtype,
                                         mepi_producttypeinfo = mp.mepi_producttypeinfo,
                                         mepi_memberproducttype = mp.mepi_memberproducttype,
                                         mepi_status = mp.mepi_status
                                     }).ToList();

        return Ok(activeInDateRange);
    }

使用 Take() 方法:

public IHttpActionResult retentionRate() {

        DateTime today = DateTime.Today;
        DateTime firstOfThisMonth = new DateTime(today.Year, today.Month, 1);
        DateTime  LastYearDate = DateTime.Today.AddDays(-365);




           var activeInDateRange = (from mp in FitnessDbo.memberproductinfoes
                                     where mp.mepi_expirydate >= LastYearDate &&
                                     mp.mepi_activationdate < today
                                     select new
                                     {
                                         mepi_memberproductinfoid = mp.mepi_memberproductinfoid,
                                         mepi_memberid = mp.mepi_memberid,
                                         mepi_activationdate = (mp.mepi_activationdate < LastYearDate ? LastYearDate : mp.mepi_activationdate),
                                         mepi_expirydate = (mp.mepi_expirydate.Value > today? today : mp.mepi_expirydate),
                                         mepi_prodtype = mp.mepi_prodtype,
                                         mepi_producttypeinfo = mp.mepi_producttypeinfo,
                                         mepi_memberproducttype = mp.mepi_memberproducttype,
                                         mepi_status = mp.mepi_status
                                     }).Take(10);
}

我收到异常

{"message":"An error has occurred.",
"exceptionMessage":"Calling 'Read' when the data reader is closed is not a valid operation.",
"exceptionType":"System.Data.Entity.Core.EntityCommandExecutionException",
"stackTrace":
   "   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper\`1.HandleReaderException(Exception e)\r\n
       at System.Data.Entity.Core.Common.Internal.Materialization.Shaper\`1.StoreRead()\r\n   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper\`1.SimpleEnumerator.MoveNext()\r\n
       at System.Data.Entity.Internal.LazyEnumerator\`1.MoveNext()\r\n
       at System.Collections.Generic.List\`1..ctor(IEnumerable\`1 collection)\r\n
       at System.Linq.Enumerable.ToList[TSource](IEnumerable\`1 source)\r\n
       at BI_leejam.Controllers.MembersController.retentionRate() in C:\Users\ahijazi\source\repos\BI_leejam\BI_leejam\Controllers\MembersController.cs:line 123\r\n
       at lambda_method(Closure , Object , Object[] )\r\n
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary\`2 arguments, CancellationToken cancellationToken)\r\n
    --- End of stack trace from previous location where exception was thrown ---\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
       at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
       at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n
--- End of stack trace from previous location where exception was thrown ---\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n
       at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
"innerException":{"message":"An error has occurred.",
    "exceptionMessage":"Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.",
    "exceptionType":"MySql.Data.MySqlClient.MySqlException",
    "stackTrace":
       "   at MySql.Data.MySqlClient.Interceptors.ExceptionInterceptor.Throw(Exception exception)\r\n
           at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)\r\n   at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex)\r\n
           at MySql.Data.MySqlClient.MySqlDataReader.Read()\r\n
           at MySql.Data.EntityFramework.EFMySqlDataReader.Read()\r\n
           at System.Data.Entity.Core.Common.Internal.Materialization.Shaper\`1.StoreRead()",
    "innerException":{"message":"An error has occurred.",
        "exceptionMessage":"Timeout in IO operation",
        "exceptionType":"System.TimeoutException",
        "stackTrace":
       "   at MySql.Data.MySqlClient.TimedStream.StopTimer()\r\n
           at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n
           at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)\r\n
           at MySql.Data.MySqlClient.MySqlStream.LoadPacket()\r\n
           at MySql.Data.MySqlClient.MySqlStream.ReadPacket()\r\n
           at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)\r\n
           at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)\r\n
           at MySql.Data.MySqlClient.ResultSet.GetNextRow()\r\n
           at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)\r\n
           at MySql.Data.MySqlClient.MySqlDataReader.Read()"}}}

尝试将此添加到您的控制器:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        if (this.FitnessDbo != null) { this.FitnessDbo.Dispose(); }
    }

    base.Dispose(disposing);
}

编辑一些解释:
从评论中的问题来看,即使控制器在请求结束时被处理,上下文使用的连接也被重用但不处于可用状态。通过在处理控制器时显式处理上下文,应该正确关闭上下文和连接,确保我们在下一个请求时获得新连接。