J'utilise entity Framework 4.1 qui interroge une base de données SQL Server 2008. Malheureusement, tous souvent nous obtenons l'exception ci-dessous:Entity Framework - System.IndexOutOfRangeException
<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>
Le code réel exemple:
//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;
}
MISE À JOUR ** Mise à jour mon code avec la mise en cache.
J'ai recherché sur Google et je n'ai pas trouvé de réponse définitive à la cause. Certains qui ont fait face à ce problème pourraient-ils partager une résolution?
Merci.
Votre contexte n'est pas synchronisé avec la base de données? (Les colonnes de base de données ne correspondent pas aux propriétés de la classe?) –
Problème se produit uniquement de façon aléatoire et je ne pense pas qu'il s'agit d'un problème de synchronisation. –
Connexe: http://stackoverflow.com/questions/12617639/weird-random-exceptions –