title: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
body: <p>I've read some questions/answers that have to do with this particular error message, but I'm not quite understanding the appropriate solution.</p>
  
  <p>I've read many times that you should create the EF4 context, use it, then dispose of it. Throughout my application, I'm loading entities here and there using different context objects, and then eventually want to associate the entities together.</p>
  
  <p>I've create a simple console application that easily causes the error. The very simple model is diagrammed followed by the code.</p>
  
  <p>How can I get the two different entities to share the same context? Do I really have to create a new context, load the two entities again (even though I already have them), simply to associate them and save?</p>
  
  <p>If I simply missed an already existing, appropriate question/answer, please point me to the right place.</p>
  
  <p><img src="https://i.stack.imgur.com/nzH9m.jpg" alt="Simple EF4 Model diagram"></p>
  
  <pre><code>internal class Program {
  
      private static void Main(string[] args) {
          DeleteAllEntities();
          CreateInitialEntities();
          Owner o = LoadOwner();
          Child c = LoadChild();
          AssociateAndSave(o, c);
      }
  
      private static void AssociateAndSave(Owner o, Child c) {
          using (var context = new ModelEntities()) {
              // Exception occurs on the following line.
              o.Children.Add(c);
              context.Attach(o);
              context.SaveChanges();
          }
      }
  
      private static Owner LoadOwner() {
          using (var context = new ModelEntities()) {
              return ((from o in context.Owners
                       select o).First());
          }
      }
  
      private static Child LoadChild() {
          using (var context = new ModelEntities()) {
              return ((from c in context.Children
                       select c).First());
          }
      }
  
      private static void CreateInitialEntities() {
          using (var context = new ModelEntities()) {
              Owner owner = new Owner();
              Child child = new Child();
              context.Owners.AddObject(owner);
              context.Children.AddObject(child);
              context.SaveChanges();
          }
      }
  
      private static void DeleteAllEntities() {
          using (var context = new ModelEntities()) {
              List&lt;Child&gt; children = (from c in context.Children
                                      select c).ToList();
              foreach (var c in children)
                  context.Children.DeleteObject(c);
              List&lt;Owner&gt; owners = (from o in context.Owners
                                    select o).ToList();
              foreach (var o in owners)
                  context.Owners.DeleteObject(o);
              context.SaveChanges();
          }
      }
  }
  </code></pre>