title: How to copy an entity from one Entity Framework context to another?
body: <p>How to copy an entity from one context (inheriting from <code>DbContext</code>) to another?</p>
  
  <p>Everything I have found up to works only for <code>ObjectContext</code> but not for the <code>DbContext</code> or uses <code>DbContext</code> but does not work.</p>
  
  <p>For example, I have found/tried:</p>
  
  <ul>
  <li>Uses ObjectContext: CloneHelper on CodeProject</li>
  <li>Setting LazyLoadingEnabled to false results in not filling the ICollection&lt;> properties (foreign-keys)</li>
  <li>Setting ProxyCreationEnabled to false results in keeping the ICollection&lt;> properties as null (foreign-keys)</li>
  <li>Entry&lt;>.State=Detached results in not filling the ICollection&lt;> properties (foreign-keys) if set before attaching the property, or blocks clearing Id if set later on.</li>
  <li>AsNoTracking() results in an exception (one of the following, depends whether inserting to the remote context first an item from the parent's ICollection&lt;> property or first the parent):
  <ul>
  <li>Parent: The object in the 'ModelFirstSub_First_Target' role cannot be automatically added to the context because it was retrieved using the NoTracking merge option. Explicitly attach the entity to the ObjectContext before defining the relationship.</li>
  <li>Item: The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.</li>
  </ul></li>
  </ul>
  
  <p>I will use it for two purposes:</p>
  
  <ol>
  <li>Copy everything from one database to another (the destination will be a clone of the original; a local copy of a remote database). Ids of objects should be preserved.</li>
  <li>Add or update selected entities from one database to another (upstreaming changes in the local cache to the remote origin). Ids of the newly created objects does not need to be persisted.</li>
  </ol>
  
  <p>How to perform that? </p>
  
  <p>EF 6.1.1, .NET 4.5.2, C#</p>
  
  <p>Here is the test code which tries to simulate the second action (upstreaming changes back to the remote database):</p>
  
  <pre><code>var addedFirst = localContext.Firsts.AsNoTracking().Single(m =&gt; m.Id == 4);
  var updatedFirst = localContext.Firsts.AsNoTracking().Single(m =&gt; m.Id == 2);
  
  addedFirst.Items.First().Id = 0;
  addedFirst.Items.First().FirstId = 0;
  addedFirst.Id = 0;
  
  remoteContext.FirstItems.Add(addedFirst.Items.First());
  remoteContext.Firsts.Add(addedFirst);
  
  var originalFirst = remoteContext.Firsts.Single(m =&gt; m.Id == 2);
  var originalItem = originalFirst.Items.First();
  originalItem.Title = updatedFirst.Items.First().Title;
  </code></pre>
  
  <p>Here is the model:</p>
  
  <pre><code>public class ModelFirstBase
  {
      public virtual int Id { get; set; }
      public virtual ICollection&lt;ModelFirstSub&gt; Items { get; set; }
      public virtual string Title { get; set; }
  }
  
  public class ModelFirstSub
  {
      public virtual int Id { get; set; }
      public virtual int FirstId { get; set; }
      public virtual ModelFirstBase First { get; set; }
      public virtual string Title { get; set; }
  }
  </code></pre>
  
  <p>PS: The properties needs to be kept virtual as the model is shared with the production app which benefits from dynamic proxies.</p>