Linq To Entities, исключение ToList () при вызове из не-пользовательского интерфейса - PullRequest
0 голосов
/ 20 апреля 2011

Я использую 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 голосов
/ 20 апреля 2011

Entity Framework не является поточно-ориентированным, вы можете использовать ObjectContext только в том потоке, который его создал.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...