title: Entity Framework trying to insert parent object
body: <p><strong>The Problem</strong></p>
  
  <p>I'm using Entity Framework to add a complex object model to the database. I'm encountering an issue where I try to insert a child object that EF is also trying to insert the parent object, which is causing an integrity issue in the database.</p>
  
  <p><strong>Explanation</strong></p>
  
  <p>For my example, let's assume we have two tables:</p>
  
  <ol>
  <li>Shop</li>
  <li>ShopPerformance</li>
  </ol>
  
  <p>A Shop can have many ShopPerformance entries, the ShopPerformance must have a Shop, and the Shop record already exists in the database (so EF should be leaving it alone and focusing on the ShopPerformance).</p>
  
  <p>In my example, I am attempting to add the ShopPerformance only (in this case the instance of ShopPerformance is called "performance":</p>
  
  <pre><code>this.db.Entry(performance).State = System.Data.EntityState.Added;
  this.db.SaveChanges();
  </code></pre>
  
  <p>When I call SaveChanges(), EF is trying to insert the Shop as well, which causes a constraint error in the database as it has constraints to prevent duplicate entries (based on the shop's name).</p>
  
  <p><strong>Things I've Tried</strong></p>
  
  <p>I've tried setting the Shop property in ShopPerformance to null to stop EF from doing things I don't want to do (it still has the ShopID as a separate property). First I tried:</p>
  
  <pre><code>Shop theShop = performance.Shop;
  performance.Shop = null;
  this.db.Entry(performance).State = System.Data.EntityState.Added;
  this.db.SaveChanges();
  performance.Shop = theShop;
  </code></pre>
  
  <p>In this scenario, EF somehow re-establishes the link between shop and performance and tries to insert shop again.</p>
  
  <p>Then I tried:</p>
  
  <pre><code>Shop theShop = performance.Shop;
  this.db.Entry(performance).Entity.Shop = null;
  this.db.Entry(performance).State = System.Data.EntityState.Added;
  this.db.SaveChanges();
  this.db.Entry(performance).Entity.Shop = theShop;
  </code></pre>
  
  <p>This causes null reference exceptions.</p>
  
  <p><strong>Desired Resolution</strong></p>
  
  <p>I'm looking for a way to insert my performance object without EF fiddling around with the Shop. This is completely breaking my work right now.</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>I want Entity Framework to Insert/Update ONLY the object I have told it to and none of the related objects. How can I do this?</p>