title: Temporary Entities with Entity Framework
body: <p>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.</p>
  
  <pre><code>public static Pathway ApplyCustomisation(Pathway p, ICollection&lt;PathwayCustomisation&gt; 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;
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>CoursePlanner.Test.PathwayTest.ApplyCustomisation:
  System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
  </code></pre>
  
  <p>Is there a way to work with Entity projections easily? Am I approaching the problem properly?</p>
  
  <p>Edit:</p>
  
  <p>I still get the exception when with just the first part of the method:</p>
  
  <pre><code>public static Pathway ApplyCustomisation(Pathway p, ICollection&lt;PathwayCustomisation&gt; c)
  {
      Pathway resultant = new Pathway();
      if (!p.PathwayModule.IsLoaded) p.PathwayModule.Load();
      foreach (PathwayModule m in p.PathwayModule)
      {
          resultant.PathwayModule.Add(m);
      }
      return resultant;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>.NET3.5, C#, VS Express 2008</p>
  
  <p>Thanks,</p>
  
  <p>Daniel</p>