title: Do I have to set foreign key properties manually when I change associations?
body: <p>I'm migrating from Linq-to-SQL to Entity Framework (4.4), using Database First with a DbContext. I'm wondering whether the following behavior is normal:</p>
  
  <pre><code>using (var e = new AgendaEntities()) {
      var store = e.Stores.First();
      var office = e.Offices.Create();
      office.Store = store; // Set association
      Console.WriteLine(office.StoreID); // shows Guid.Empty, expected store.ID!
  }
  </code></pre>
  
  <p>In L2S, setting the <code>Store</code> association to an entity would also update the <code>StoreID</code> key. In EF, this doesn't seem to be happening. This is regardless of whether the entities are new or loaded from the context.</p>
  
  <p>When I <code>SaveChanges</code>, it saves correctly and the <code>StoreID</code> is updated to match <code>office.ID</code>, but why does this only happen after the save?</p>
  
  <p>Is there something I'm missing, or am I now supposed to keep foreign keys in sync manually?</p>
  
  <hr>
  
  <p><strong>Solution Edit:</strong> 
  This is called property fixup, and used to be done automatically by the generated proxies. However, with <code>DbContext</code> this is no longer the case. According to <a href="http://connect.microsoft.com/VisualStudio/feedback/details/760609/poco-navigation-proxies-should-fixup-the-other-end-automatically">this Connect issue</a>, this is by design.</p>
  
  <blockquote>
    <p>Hello,
     The DbContext template actually doesn't generate classes that will be used as change tracking proxies - just lazy loading proxies (which don't do fix-up). We made this decision because change tracking proxies are complex and have a lot of nuances that can be very confusing to developers.
     If you want fix-up to occur before SaveChanges you can call myContext.ChangeTracker.DetectChanges.
     ~EF Team</p>
  </blockquote>
  
  <p>An alternative is to call <code>DbContext.Entry(entity)</code>, which will sync up the entity. This is described in this article: <a href="http://msdn.microsoft.com/en-us/data/jj713564.aspx">Relationships and Navigation Properties</a> under "Synchronizing the changes between the FKs and Navigation properties"</p>