views:

433

answers:

1

I have an EntityObject named Pathway which relates directly to data in the pathway table. My database also stores rules for Pathway customisations. What I would like to do is create a Pathway object in my code that is the resultant of the Pathway + PathwayCustomisations. The resultant should never find it's way back to the database, it is simply a temporary projection used in the code.

public static Pathway ApplyCustomisation(Pathway p, ICollection<PathwayCustomisation> c)
{
    Pathway resultant = new Pathway();
    if (!p.PathwayModule.IsLoaded) p.PathwayModule.Load();
    foreach (PathwayModule m in p.PathwayModule)
    {
        resultant.PathwayModule.Add(m);
    }
    foreach (PathwayCustomisation i in c)
    {
        switch (i.Command)
        {
            case "ADD":
                resultant.PathwayModule.Add(i.PathwayModule);
                break;
            case "DELETE":
                resultant.PathwayModule.Remove(i.PathwayModule);
                break;
        }
    }
    return resultant;
}

This method chokes at the first hurdle because I am adding PathwayModule entities to a second Pathway when they can only belong to one in the model/database:

CoursePlanner.Test.PathwayTest.ApplyCustomisation:
System.InvalidOperationException : Collection was modified; enumeration operation may not execute.

Is there a way to work with Entity projections easily? Am I approaching the problem properly?

Edit:

I still get the exception when with just the first part of the method:

public static Pathway ApplyCustomisation(Pathway p, ICollection<PathwayCustomisation> c)
{
    Pathway resultant = new Pathway();
    if (!p.PathwayModule.IsLoaded) p.PathwayModule.Load();
    foreach (PathwayModule m in p.PathwayModule)
    {
        resultant.PathwayModule.Add(m);
    }
    return resultant;
}

The above enumeration is not modifying the same collection that is being enumerated, it is simply adding the items to a second collection. This code gives the same exception.

.NET3.5, C#, VS Express 2008

Thanks,

Daniel

A: 

Your exception is caused by the fact that you are modifing a collection while enumerating it.

foreach (Item item in collection)
{
    collection.Add(something); // Not allowed.
    collection.Remove(something); // Not allowed, too.
}

Working with entities like normal objects is no problem - you must only avoid that the entities are attached to an object context (directly or indirectly through related entities) and they will not be persisted to the database.

Daniel Brückner
I though that at first and I realise that is a problem with the method but I still get the exception with just the first part of the method (see my edit above). That enumeration is not modifying the same collection that is being enumerated, it is simply adding the items to a second collection. This code gives the same exception.
In creating the new resultant Pathway I do not in any way wish to permanently associate any PathwayModules with the resultant or modify any of the existing Entities and their relationships in any way.
I don't know your model, but does this involve a many-to-many relation ship? Adding something to one end of the relaltionship will change the other end, too.
Daniel Brückner
Yes I think that's the problem. A PathwayModule entity cannot belong to more than one Pathway entity. The resultant Pathway I am looking to create is not an entity, but should behave just like a Pathway.I must be approaching the problem incorrectly...