title: Entity Framework 5 Foreign Key New Record on SaveChanges
body: <p>I'm using .NET4.5/EF5 and have created the model from an existing database.</p>
  
  <p>I'm using the following code:</p>
  
  <pre><code>Order currentOrder = new Order();
  using (var db = new ILSEntities())
  {
      try
      {
          Event currentEvent = db.Events.OrderByDescending(u =&gt; u.EventID).FirstOrDefault();
          currentOrder.Event = currentEvent;
          db.Orders.Add(currentOrder);
  
          db.SaveChanges();
  </code></pre>
  
  <p>And I'm seeing that a duplicate record is being created of the Event object I find, which is not what I wanted to happen.</p>
  
  <p>I've read a lot of posts relating to similar problems, but where the context of the two participants in the foreign key relationships are different. Here, I'm saving with the same context I use to find one, and the other object is new.</p>
  
  <p>I've also tried:</p>
  
  <pre><code>currentOrder.Event.EventID = currentEvent.EventID;
  </code></pre>
  
  <p>but that fails as well as I get an EF validation error telling me it needs values for the other members of the Event object.</p>
  
  <p>I've also tried specifically setting the <code>EntityState</code> of the object being duplicated to <code>Detached</code>, <code>Modified</code> etc. after adding the <code>Order</code> object but before <code>SaveChanges</code> without success.</p>
  
  <p>I'm sure this is a basic problem, but it's got me baffled</p>