叫我用EF 4.0和它的作品真的很好,當異常,但問題發生時 我打電話查詢:LINQ到實體,ToList()從非UI線程
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)
你在創建查詢和枚舉它之間做了什麼?這是推遲的,所以你以前做的任何事情都不會發生,直到你試圖枚舉。 – 2011-04-20 08:28:50
你是對的,但這正是我使用的代碼... – Robertok 2011-04-20 08:34:17