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