请各位大神来看看啊,用EF查询数据,偶尔会报错,报错基本是下面三种类型 ,找了几天了,没找到原因,在线等,挺急的  重点是偶尔、偶尔、偶尔,分不多,请帮帮忙/*******************************************************************************/
日志时间:2018-01-20 02:57:18,652 [13] 
日志级别:INFO  
日 志 类:loginfo [(null)];
错误消息:未将对象引用设置到对象的实例。
控制器:User 
方法:GetApply
堆栈信息:   在 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   在 System.Data.SqlClient.SqlConnection.Open()
   在 System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   在 System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   在 System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
   在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   在 DaiYu.Controllers.UserController.GetApply(Int32 userID, Int32 gameID)
   在 lambda_method(Closure , Object , Object[] )
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext() 
 <HR Size=1>
/*******************************************************************************//*******************************************************************************/
日志时间:2018-01-20 10:12:48,031 [61] 
日志级别:INFO  
日 志 类:loginfo [(null)];
错误消息:The underlying provider failed on Open.
控制器:User 
方法:GetApply
堆栈信息:   在 System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   在 System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
   在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   在 DaiYu.Controllers.UserController.GetApply(Int32 userID, Int32 gameID)
   在 lambda_method(Closure , Object , Object[] )
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext() 
 <HR Size=1>
/*******************************************************************************/
/*******************************************************************************/
日志时间:2018-01-08 03:34:47,923 [11] 
日志级别:INFO  
日 志 类:loginfo [(null)];
错误消息:An error occurred while executing the command definition. See the inner exception for details.
控制器:User 
方法:GetApply
堆栈信息:   在 System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   在 System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   在 DaiYu.Controllers.UserController.GetApply(Int32 userID, Int32 gameID)
   在 lambda_method(Closure , Object , Object[] )
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() <HR Size=1>
/*******************************************************************************/

解决方案 »

  1.   

    未将对象引用设置到对象的实例
    不知道你的getapply方法的具体做法。
    但是一般就是某个对象为空了。是不是数据库偶尔被删除数据导致的?
      

  2.   


    没有删,重新生成一次解决方案就好了,一般是查询数据的时候出问题,例如像 
    if (user == null)
                            {
                                user = userService.GetModels(p => p.ID == userID).FirstOrDefault();}这样
    好像比较容易出问题的是后面加了FirstOrDefault() 这个的查询语句,我怀疑是不是有时候上下文被释放了,所以查不到数据
     public static DaiYuEntities Create()
            {
                DaiYuEntities dbContext = HttpContext.Current.Items["dbContext"] as DaiYuEntities;
                if (dbContext == null)
                {
                    dbContext = new DaiYuEntities();
                    HttpContext.Current.Items["dbContext"] = dbContext;            }
                return dbContext;  
            }
    我是这样生成的,有没有问题呢
      

  3.   


    没有删,重新生成一次解决方案就好了,一般是查询数据的时候出问题,例如像 
    if (user == null)
                            {
                                user = userService.GetModels(p => p.ID == userID).FirstOrDefault();}这样
    好像比较容易出问题的是后面加了FirstOrDefault() 这个的查询语句,我怀疑是不是有时候上下文被释放了,所以查不到数据
     public static DaiYuEntities Create()
            {
                DaiYuEntities dbContext = HttpContext.Current.Items["dbContext"] as DaiYuEntities;
                if (dbContext == null)
                {
                    dbContext = new DaiYuEntities();
                    HttpContext.Current.Items["dbContext"] = dbContext;            }
                return dbContext;  
            }
    我是这样生成的,有没有问题呢你试试这样获取dbcontext
    public class DbContextFactory  
        {  
            public static ELDBEntity GetCurrentContext()  
            {  
                ELDBEntity _nContext = CallContext.GetData("ELDBEntity") as ELDBEntity;  
                if (_nContext == null)  
                {  
                    _nContext = new ELDBEntity();  
                    CallContext.SetData("ELDBEntity", _nContext);  
                }  
                  
                return _nContext;  
            }  
        }  
      

  4.   

            public static DaiYuEntities Create()
            {
                //DaiYuEntities dbContext = CallContext.GetData("DbContext") as DaiYuEntities;
                //if (dbContext == null)
                //{
                //    dbContext = new DaiYuEntities();
                //    CallContext.SetData("DbContext", dbContext);
                //}
                //return dbContext;
                DaiYuEntities dbContext = HttpContext.Current.Items["dbContext"] as DaiYuEntities;
                if (dbContext == null)
                {
                    dbContext = new DaiYuEntities();
                    HttpContext.Current.Items["dbContext"] = dbContext;            }
                return dbContext;  
            }这个我也试过
      

  5.   

    那你先把dal层的using块去掉试试看。
    我之前也遇到过类似的问题。
    using(....){}先去掉看看呢
      

  6.   

    那你先把dal层的using块去掉试试看。
    我之前也遇到过类似的问题。
    using(....){}先去掉看看呢
    namespace Dal
    {    public class BaseDal<T> : IBaseDal<T> where T : class, new()
        {
            protected DaiYuEntities dbContext = DbContextFactory.Create();
            public void Add(T t)
            {
                dbContext.Set<T>().Add(t);
            }
            public void Delete(Expression<Func<T, bool>> whereLambda)
            {
                dbContext.Set<T>().Delete(whereLambda);
            }        public void Update(T t)
            {
                dbContext.Set<T>().AddOrUpdate(t);
            }
            public List<T> GetModels()
            {
                return dbContext.Set<T>().AsNoTracking().ToList();
            }
            public IQueryable<T> GetModels(Expression<Func<T, bool>> whereLambda)
            {
                return dbContext.Set<T>().AsNoTracking().Where(whereLambda);
            }
            public bool SaveChanges()
            {
                return dbContext.SaveChanges() > 0;
            }
        }
    }Dal层的基类是这样的 好像没有用using,是否有问题
      

  7.   

    这个没问题。
    目前我也猜不出具体原因
    你写日志时,记录一下  innerException的信息。
      

  8.   

    /*******************************************************************************/
    日志时间:2018-01-25 18:02:34,873 [268] 
    日志级别:INFO  
    日 志 类:loginfo [(null)];
    错误消息:基础提供程序在 Open 上失败。
    控制器:Order 
    方法:GetOrderEvaluate
    参数:orderID=386|userID=18|
    堆栈信息:   在 System.Data.Entity.Core.EntityClient.EntityConnection.Open()
       在 System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
       在 System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       在 System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
       在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       在 System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       在 System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
       在 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       在 DaiYu.Controllers.OrderController.GetOrderEvaluate(Int32 orderID, Int32 userID) 位置 e:\带鱼电竞\DaiYu\DaiYu\Controllers\OrderController.cs:行号 762
       在 lambda_method(Closure , Object , Object[] )
       在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext()
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.Filters.ActionFilterAttribute.<ExecuteActionFilterAsyncCore>d__0.MoveNext()
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext() 
     <HR Size=1>
    /*******************************************************************************/
    就这样的
      

  9.   

    看你用的是全局上下文(HttpContext.Current.Items["dbContext"] as DaiYuEntities),这在IIS环境下,不是作死吗?
    IIS会回收全局变量。
    我想你肯定可以说,你已经用了
    if (dbContext == null)
                {
                    dbContext = new DaiYuEntities();但你这并不是原子操作。正因为如此,问题才会偶发。
      

  10.   

    错误:基础提供程序在 Open 上失败
    那应该是打开连接失败了。
    感觉你的错误好像不是同一个吧?
      

  11.   

    那请问你要如何处理呢
    不要用全局上下文,每次都用到再new