none
Entity Framework - System.IndexOutOfRangeException RRS feed

  • Question

  • 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>
    The actual sample code:

    //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;
    }

    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.

    Thank you.

    Wednesday, September 26, 2012 11:36 PM

All replies

  • Anyone has any suggestion, please
    Thursday, September 27, 2012 12:01 PM
  • Hello, it seems that you have a trouble with the columns indexes of your mapped entities. Are you using Code First or Model First?

    Regards,

    Miguel.

    Thursday, September 27, 2012 12:09 PM
  • Hi,

    If the exception is thrown at this point

    entityResult.First<Employee>(); then

    replace it with

    entityResult.FirstOrDefault<Employee>();

    Thanks,

    BR,

    JollyWagoner

    Thursday, September 27, 2012 5:07 PM
  • Thanks but why would this error occurred on code I provided and how would the new code you've provided would solve the problem.
    Friday, September 28, 2012 1:20 PM
  • Database first thanks Miguel
    Friday, September 28, 2012 1:20 PM
  • Hello again, It seems that you changed your database model without update your DbModel/DbContext. Try updating it.

    Hope this helps,

    Miguel.

    Friday, September 28, 2012 1:28 PM
  • Thanks Miguel,I checked every single bit of it an nothing has been changed. The issue occurs randomly but not always. i suspect its a cache and deferred execution of IQuerable?
    • Edited by suzzane Saturday, September 29, 2012 3:46 AM
    Friday, September 28, 2012 11:02 PM
  • Hello again suzzane, I recomend you to make cache of the result of the query. Don't make cache of the query itself, instead of that, you should make cache of the executed query, for example calling the ToList() method of the IQueryable interface.

    Try that and tell me if you are getting the same behavior.

    Regards,

    Miguel.

    Monday, October 1, 2012 11:31 AM
  • Thanks Miguel, any idea on how cache would cause the above exception to be thrown by EF LINQ?
    Wednesday, October 3, 2012 12:24 PM
  • Hello again, I'm not sure but it seems that there is a problem with the EF metadata. Maybe you are using LINQ to entities query created from a disposed DbContext. Is for that I recommended you to catch the query results, not the query itself.

    Regards,

    Miguel.

    Wednesday, October 3, 2012 1:23 PM