title: How to create foreign key relationships with the Entity Framework?
body: <p>I want to create a new row in my database on a table that has a couple of foreign key relationships and I haven't been able to get a handle on what order and what calls need to be made. This is what I have so far:</p>
  
  <pre><code>db.Models.Order order = DB.Models.Order.CreateOrder( apple );
  order.CustomerReference.Attach( ( from c in db.Customer where c.Id == custId select c ).First() );
  db.SaveChanges();
  </code></pre>
  
  <p>The code is failing on the second line there, saying: </p>
  
  <blockquote>
    <p>Attach is not a valid operation when
    the source object associated with this
    related end is in an added, deleted,
    or detached state. Objects loaded
    using the NoTracking merge option are
    always detached.</p>
  </blockquote>
  
  <p>Any ideas?</p>