InvalidOperation in T4 Generated Helper Method SelfTrackingModel.Context.Extensions

436 views Asked by At

I have a project whereby I'm using STE's (EF4.1) and I'm hitting a reproducable issue when I call the auto generated ApplyChanges() method on one of my object sets, passing in an object graph I have held in memory, and I'm getting the following error:

InvalidOperationException: 
    Collection was modified; enumeration operation may not execute.

This is originating from within a call to ObjectContext.AddObject() method, which is being called by the T4 auto generated ApplyChanges() method. My method to call ApplyChanges looks something like this:

// 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();
        }
    }
}

The call stack when I hit the error looks something like this:

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)

I'm not modifying the object graph at all whilst the operation is in progress, so I'm really stumped as to the issue. Anybody got any ideas?

Thanks

2

There are 2 answers

0
relascope On

We had the same problem. We could solve it with removing Navigation properties so that they cannot run in circles. So if A has B as a Navigation Property, B should not hav A as a Navigation property. It is not possible under all circumstances but in our case, it solved the problem.

0
alesterre On

Had the same problem. Looks like a bug that was fixed in .net 4.5.