我有一个项目,我正在使用STE(EF4.1),当我在我的一个对象集上调用自动生成的ApplyChanges()方法时,我遇到了一个可重现的问题,传入了一个我有的对象图保存在内存中,我收到以下错误:
InvalidOperationException:
Collection was modified; enumeration operation may not execute.
这源于对ObjectContext.AddObject()方法的调用,该方法由T4自动生成的ApplyChanges()方法调用。我调用ApplyChanges的方法如下所示:
// Extension to auto generated Session : IObjectWithChangeTracker
public partial class Session
{
public void SaveToDatabase(String myConnectionString)
{
using (MyContext DbContext = new MyContext(myConnectionString))
{
DbContext.Sessions.ApplyChanges(this);
DbContext.SaveChanges();
}
}
}
我遇到错误时的调用堆栈看起来像这样:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Data.Objects.DataClasses.RelatedEnd.WalkObjectGraphToIncludeAllRelatedEntities(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
... // The below 4 lines are pretty much repeated as it iterates through the object graph
at System.Data.Objects.DataClasses.RelatedEnd.WalkObjectGraphToIncludeAllRelatedEntities(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.EntityCollection`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.WalkObjectGraphToIncludeAllRelatedEntities(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.AddGraphToObjectStateManager(IEntityWrapper wrappedEntity, Boolean relationshipAlreadyExists, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelatedEnd.IncludeEntity(IEntityWrapper wrappedEntity, Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.EntityReference`1.Include(Boolean addRelationshipAsUnchanged, Boolean doAttach)
at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
at MyNamespace.SelfTrackingEntitiesContextExtensions.AddHelper.AddAllEntities(ObjectContext context, String entitySetName, IObjectWithChangeTracker entity)
at MyNamespace.SelfTrackingEntitiesContextExtensions.ApplyChanges[TEntity](ObjectContext context, String entitySetName, TEntity entity)
at MyNamespace.SelfTrackingEntitiesContextExtensions.ApplyChanges[TEntity](ObjectSet`1 objectSet, TEntity entity)
at MyNamespace.Transaction.SaveToDatabase(String transactionDbConnectionString)
当操作正在进行时,我根本没有修改对象图,所以我真的很难过这个问题。有人有任何想法吗?
由于
答案 0 :(得分:0)
我们遇到了同样的问题。 我们可以通过删除导航属性来解决它,以便它们不能以圆圈运行。 因此,如果A将B作为导航属性,则B不应该将A作为导航属性。 在所有情况下都不可能,但在我们的情况下,它解决了这个问题。
答案 1 :(得分:0)
有同样的问题。看起来像是在.net 4.5中修复的错误。