实体框架-从非ui线程调用Linq到实体,ToList()异常



我使用EF 4.0,它工作得很好,但是问题发生时我调用query:


var query = from employee in employees from cr in employee.RcpCrossRegister select new { employee, cr } 当我调用

var list = query.ToList()

I got exception

    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:VS2010CaliburnMicrocaliburnmicro_a63379fba70aTCS.2.3TCS.CoreCaliburnInvocationBackgroundTask.cs:wiersz 41
   w System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   w System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

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

相关内容

  • 没有找到相关文章

最新更新