title: Transfer ForeignKey relationship when copying entities
body: <p>I have two entities (<code>MediaFileDescriptor</code> and <code>TransformationTest</code>) with foreign-key relationships to two different subsets of another entity (<code>MediaFileMetaData</code>) collection.</p>
  
  <p>So, a subset of my <code>MediaFileMetaData</code> collection is linked to a <code>MediaFileDescriptor</code> and another subset of  my <code>MediaFileMetaData</code> collection is linked to <code>TransformationTest</code>.</p>
  
  <p><strong>TransformationTest.cs:</strong></p>
  
  <pre><code>public class TransformationTest : IEntity
  {
      [Key]
      public int Id { get; set;
      public virtual ICollection&lt;MediaFileMetaData&gt; MediaFileMetaDatas { get; set; }
  
      // some more properties (removed for readability)
   }
  </code></pre>
  
  <p><strong>MediaFileDescriptor.cs:</strong></p>
  
  <pre><code>public class MediaFileDescriptor : IEntity
  {
      [Key]
      public int Id { get; set; }
      public virtual ICollection&lt;MediaFileMetaData&gt; MediaFileMetaDatas { get; set; }
  
      // some more properties (removed for readability)
  }
  </code></pre>
  
  <p><strong>MediaFileMetaData.cs:</strong></p>
  
  <pre><code>public class MediaFileMetaData : IEntity
  {
      [Key]
      public int Id { get; set; }
  
      public int TransformationTestId { get; set; }
      public virtual TransformationTest TransformationTest { get; set; }
  
      public int MediaFileDescriptorId { get; set; }
      public virtual MediaFileDescriptor MediaFileDescriptor { get; set; }
  
      // some more properties (removed for readability) 
  }
  </code></pre>
  
  <p>I am now creating copies of these entities. After copying <code>TransformationTest</code> and the linked <code>MediaFileMetaData</code>, I want to create a copy of the <code>MediaFileDescriptor</code>. I don't want to replicate the <code>MediaFileMetaData</code> again, but want to link the entities that I just replicated in the previous step with my new copy of <code>MediaFileDescriptor</code>. But since my new <code>MediaFileDescriptor</code> has a new <code>id</code>, the copied <code>MediaFileMetaData</code>s are referencing to the original <code>MediaFileDescriptor</code>.</p>
  
  <p>So, my problem is: How can I tell the entity framework which subset of my <code>MediaFileMetaData</code> collection (e.g. all entities with the old <code>MediaFileDescriptor.Id</code>) are linked to my new <code>MediaFileDescriptor</code>?</p>
  
  <p>Here are my relationship specifications (not sure whats the correct name):</p>
  
  <pre><code>modelBuilder.Entity&lt;TransformationTest&gt;()
      .HasMany&lt;MediaFileMetaData&gt;(c =&gt; c.MediaFileMetaDatas)
      .WithRequired(c =&gt; c.TransformationTest)
      .HasForeignKey(c =&gt; c.TransformationTestId)
      .WillCascadeOnDelete(true);
  
  modelBuilder.Entity&lt;MediaFileDescriptor&gt;()
      .HasMany&lt;MediaFileMetaData&gt;(c =&gt; c.MediaFileMetaDatas)
      .WithRequired(c =&gt; c.MediaFileDescriptor)
      .HasForeignKey(c =&gt; c.MediaFileDescriptorId)
      .WillCascadeOnDelete(false);
  </code></pre>
  
  <p>This is my (not working) code:</p>
  
  <pre><code>var newTests = source.TransformationTests.Select(
      c =&gt; new TransformationTest
          {
              // replicating other properties
              MediaFileMetaDatas = c.MediaFileMetaDatas.Select(m =&gt; new MediaFileMetaData { Data = m.Data }).ToList()
          });
  
  var newFileDescriptors = source.MediaFileDescriptors.Select(
      c =&gt; new MediaFileDescriptor
          {
              // replicating other properties
              MediaFileMetaDatas =  // what should go here?
          });
  </code></pre>
  
  <p>Unfortunately me program is to complex to give a minimal working example ...</p>