title: Cloning Object with many-to-many relationship in EntityFramework
body: <p>All I want is just to create an exact copy of an object.</p>
  
  <p>I have a class</p>
  
  <pre><code>[Serializable]
  public class Project 
  {
      public int Id { get; set; }
      public String Name { get; set; }
  
      //navigational fields..
      public virtual List&lt;BusinessRequirement&gt; BusinessRequirements { get; set; }
   }
  </code></pre>
  
  <p>and another</p>
  
  <pre><code>[Serializable]
  public class BusinessRequirement 
  {
     public int Id { get; set; }
     public String Name { get; set; }
     public String Description { get; set; }
     public virtual List&lt;Project&gt; Projects { get; set; }
  }
  </code></pre>
  
  <p>so somewhere I've configured the many-to-many relationship b/w <code>Project</code> and <code>BusinessRequirement</code> like this:</p>
  
  <pre><code>HasMany(s =&gt; s.BusinessRequirements)
             .WithMany(s =&gt; s.Projects)
             .Map(m =&gt;
              {
                     m.MapLeftKey("ProjectId");
                     m.MapRightKey("BusinessRequirementId");
                     m.ToTable("ProjectBusinessRequirementMapping");
  
              });
  </code></pre>
  
  <p>and also I've made my dbcontext static i.e.</p>
  
  <pre><code>public static class DataLayer
  {
      public static MyDbContext db;
  }
  </code></pre>
  
  <p>now, all m doing is, trying to make a copy of an object of <code>Project</code> i.e.</p>
  
  <pre><code>public Project Clone(Project source)
  {
  
       Project  target = new Project();
       target.Name = source.Name;
       //1.
       // target = source;    
  
       //2.
       target.BusinessRequirements = new List&lt;BusinessRequirement&gt;();
       foreach(BusinessRequirement br in source.BusinessRequirements)
       {
         BusinessRequirement nbr = DataLayer.Get&lt;BusinessRequirement&gt;(s=&gt;s.Id=br.Id).SingleOrDefault();
          if(nbr!=null)
            target.BusinessRequirements.Add(nbr);
       }  
  
  
       //3.
       //target.BusinessRequirements = source.BusinessRequirements;
  
       //4.
       //target.BusinessRequirements = new List&lt;BusinessRequirement&gt;();
       //foreach(BusinessRequirement br in source.BusinessRequirements)
       //{
       //  BusinessRequirement nbr = br;
       //   if(nbr!=null)
       //     target.BusinessRequirements.Add(nbr);
       //}  
  
        return target;
  }
  </code></pre>
  
  <p>none of the four methods work properly.</p>
  
  <p>the one which is closest to working is 2, but a strange thing happens. 
  Now, If i add any BusinessRequirements to <code>Original Project</code>, it also gets added to <code>Clonned One</code> and vice-versa, same goes for deletion.</p>
  
  <p>Somehow, entityframework is treating both the projects as one. Though this behavior occurs only in many-to-many related navigational properties. </p>
  
  <p>Why EntityFramework is behaving like this???. What am I missing? please help..</p>
  
  <p>Its been almost a day, but I cannot get it to work.</p>
  
  <p>I have tried <a href="http://ox.no/posts/extension-methods-for-copying-or-cloning-objects" rel="nofollow noreferrer">this</a>, <a href="http://www.codeproject.com/Articles/137853/Cloning-the-Entity-object-and-all-related-children" rel="nofollow noreferrer">this</a>, <a href="https://stackoverflow.com/questions/7172725/copy-entity-framework-object">this</a> and <a href="https://github.com/havard/copyable" rel="nofollow noreferrer">this</a> but they didn't work either..</p>