title: Entity Framework - System.IndexOutOfRangeException
body: <p>I'm using entity framework 4.1 which queries a SQL Server 2008 database. Unfortunately every often we get the exception below:</p>
  
  <pre><code>&lt;ExceptionType&gt;System.IndexOutOfRangeException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/ExceptionType&gt;
    &lt;Message&gt;Index was outside the bounds of the array.&lt;/Message&gt;
  
  
  
  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)&lt;/StackTrace&gt;
  </code></pre>
  
  <p>The actual sample code:</p>
  
  <pre><code>//Caller
    public EmployeeEntity GetEmployeeDetail(int employeeID)
      {
           IQueryable&lt;Employee&gt; result=null;
           if (myCaching.Contains("Employee"))
              {
                  result = (IQueryable&lt;Employee&gt;)myCaching["Employee"];
              }
              else
              {
                  result = dataAccess.GetEmployeeDetail();
                  myCaching.AddToCache("Employee", result); //Expire in 2min
              }
  
              IQueryable&lt;Employee&gt; entityResult = from entity in result
                                                           where entity.employeeId == employeeID
                                                           select entity;
         if (entityResult.Count&lt;Employee&gt;() &gt; 0)
                  return entityResult.First&lt;Employee&gt;();
              return new EmployeeEntity();
     }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>//DAL
  public IQueryable&lt;Employee&gt; GetEmployeeDetail()
  {
      DatabaseEntities ent = new DatabaseEntities(this._connectionString);
      IQueryable&lt;Employee&gt; result = from employee in ent.EmployeeEntity
                                             select employee;
  
      return result;
  }
  </code></pre>
  
  <p><strong>UPDATE**</strong>
  Updated my code with caching.</p>
  
  <p>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.</p>
  
  <p>Thank you.</p>