从非UI线程调用时,Linq To Entities,ToList()异常

时间:2011-04-20 08:25:15

标签: entity-framework linq-to-entities thread-safety tolist

我使用EF 4.0并且它工作得很好,但是当出现问题时 我打电话给查询:


var query = from employee in employees from cr in employee.RcpCrossRegister select new { employee, cr } 然后当我打电话

var list = query.ToList()

我得到了异常

    Collection was modified; enumeration operation may not execute
and call stack: 

       w System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   w System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   w System.Collections.Generic.List`1.Enumerator.MoveNext()
   w System.Data.Objects.DataClasses.EntityCollection`1.CheckIfNavigationPropertyContainsEntity(IEntityWrapper wrapper)
   w System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedTarget, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists, Boolean allowModifyingOtherEndOfRelationship, Boolean forceForeignKeyChanges)
   w System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWrapper wrappedEntity, Boolean applyConstraints)
   w System.Data.Objects.DataClasses.EntityReference`1.set_ReferenceValue(IEntityWrapper value)
   w System.Data.Objects.DataClasses.EntityReference.SetEntityKey(EntityKey value, Boolean forceFixup)
   w System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal(EntityReference relatedEnd, EntityKey foreignKey, Boolean setIsLoaded, Boolean replaceExistingRef)
   w System.Data.Objects.EntityEntry.FixupReferencesByForeignKeys(Boolean replaceAddedRefs)
   w System.Data.Objects.ObjectStateManager.FixupReferencesByForeignKeys(EntityEntry newEntry, Boolean replaceAddedRefs)
   w System.Data.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
   w System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   w lambda_method(Closure , Shaper )
   w System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   w System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   w TCS.bos.Infrastructure.Data.MainModule.Repositories.EmployeeRepository.GetEmployeesEvacuationTimeSheet(Int64 dateHour)
   w TCS.bos.Domain.MainModule.Employees.EmployeeTimeSheetService.GetEmployeesEvacuationTimeSheet(Int64 dateHour)
   w TCS.plg.TimeSheets.ViewModels.TimeSheetsGridViewModel.GetAll2()
   w TCS.plg.TimeSheets.ViewModels.TimeSheetsGridViewModel.<>c__DisplayClass4.<FilterCommand>b__2()
   w TCS.Core.Caliburn.Invocation.BackgroundTask.<>c__DisplayClassd.<.ctor>b__4(Object s, DoWorkEventArgs e) w D:\VS2010\CaliburnMicro\caliburnmicro_a63379fba70a\TCS.2.3\TCS.Core\Caliburn\Invocation\BackgroundTask.cs:wiersz 41
   w System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   w System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

1 个答案:

答案 0 :(得分:0)

实体框架不是线程安全的,您只能在创建它的线程上使用ObjectContext