实体框架 - System.IndexOutOfRangeException [英] Entity Framework - System.IndexOutOfRangeException

查看:263
本文介绍了实体框架 - System.IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4.1查询SQL Server 2008数据库。不幸的是,我们经常遇到以下异常:

I'm using entity framework 4.1 which queries a SQL Server 2008 database. Unfortunately every often we get the exception below:

<ExceptionType>System.IndexOutOfRangeException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
  <Message>Index was outside the bounds of the array.</Message>



at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
   at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
   at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source)
   at OnlineSelfService.Business.ContentServiceBusiness.GetPageContent(Int32 pageId)</StackTrace>

实际示例代码:

//Caller
  public EmployeeEntity GetEmployeeDetail(int employeeID)
    {
         IQueryable<Employee> result=null;
         if (myCaching.Contains("Employee"))
            {
                result = (IQueryable<Employee>)myCaching["Employee"];
            }
            else
            {
                result = dataAccess.GetEmployeeDetail();
                myCaching.AddToCache("Employee", result); //Expire in 2min
            }

            IQueryable<Employee> entityResult = from entity in result
                                                         where entity.employeeId == employeeID
                                                         select entity;
       if (entityResult.Count<Employee>() > 0)
                return entityResult.First<Employee>();
            return new EmployeeEntity();
   }

}

//DAL
public IQueryable<Employee> GetEmployeeDetail()
{
    DatabaseEntities ent = new DatabaseEntities(this._connectionString);
    IQueryable<Employee> result = from employee in ent.EmployeeEntity
                                           select employee;

    return result;
}

UPDATE **
代码与缓存。

UPDATE** Updated my code with caching.

我googled找到和回答,但找不到一个确定的答案的根本原因。

I googled to find and answer but could not find a definitive answer the root cause. Could some who have faced this issue share a resolution.

推荐答案

调用.Count()执行查询。我不会认为.First()会再次执行它,但也许它是,并且这些调用之间的事情已经改变。您可以尝试将查询重写为:

Calling .Count() executes the query. I wouldn't think the .First() would execute it again but maybe it is, and something has changed in between these calls. You could try rewriting the query as:

(from entity in result
where entity.employeeId == employeeID
select entity).FirstOrDefault() ?? new EmployeeEntity();

这篇关于实体框架 - System.IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆