El modelo de autorreferencia en EF Code First 4.1 arroja la excepción 'La colección fue modificada'

I am trying to model the following using EF 4.1 and cannot get past this exception ("Collection was modified; enumeration operation may not execute").


public class Workflow
    public List<WorkflowStage> Stages { get; set; }

public class WorkflowStage
    public virtual List<WorkflowNextStage> WorkflowNextStages { get; set; }

public abstract class WorkflowNextStage
    public virtual WorkflowStage NextStage { get; set; }

public class SuccessStage : WorkflowNextStage

public class FailureStage : WorkflowNextStage


            .HasMany(x => x.WorkflowNextStages)
            .Map(m => m.MapKey("CurrentStageId"));

            .HasRequired(x => x.NextStage)
            .Map(x => x.MapKey("NextStageId"))

Failing code:

    using (var ctx = new SBContext())
            var workflow = new Workflow();
            var stage = new WorkflowStage();
            stage.WorkflowNextStages = new List<WorkflowNextStage>
                                               new SuccessStage() {NextStage = stage},
                                               new FailureStage() {NextStage = stage}

            workflow.Stages = new List<WorkflowStage> {stage};


Setting the 'new SuccessStage' above to a different stage works just fine.

I am a bit stumped on this one...anyone have any ideas?

preguntado el 27 de agosto de 11 a las 16:08

Would love to help, please paste the entire stack trace as it may be helpful here. What is the whole error you receive and at what line is it breaking? -

1 Respuestas

Do you have a foreach loop where you are iterating through the collection you are modifying? If so this may cause this error.

Respondido el 10 de diciembre de 12 a las 08:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.