title: Linq problem with inserting new rows that have references to existing records
body: <p>(I believe this is the same problem as <a href="https://stackoverflow.com/questions/785291/why-is-linq-to-sql-entity-association-creating-a-new-duplicate-row-when-inserti">this one</a>, but there's no answer there, and I think I can express the problem better here...)</p>
  
  <p>I have two Linq-to-SQL classes, <code>State</code> and <code>County</code>, where <code>County</code> has a FK to <code>State</code>.  Here's some test code:</p>
  
  <pre><code>State s = State.GetState("NY"); // here I do a load of a State class via the Linq DataContext
  County c = new County();
  c.Name = "Rockland";
  c.State = s;
  MyDataContext.GetTable&lt;County&gt;().InsertOnSubmit(c);
  MyDataContext.SubmitChanges(); // throws an exception
  </code></pre>
  
  <p>The exception thrown is <code>"Violation of PRIMARY KEY constraint 'PK_State'. Cannot insert duplicate key in object 'dbo.State'"</code>.</p>
  
  <p>In other words, what appears to be happening here is that despite my having loaded <code>s</code> as an existing record, when I attempt to insert <code>c</code>, Linq is assuming that all related objects, <code>State</code> included, also need to be inserted!</p>
  
  <p>This is completely absurd, and I cannot believe that Microsoft would have made such a huge blunder - so it must be that somewhere my own understanding is faulty.</p>
  
  <p>Can anyone please explain what I am doing wrong, and what the correct approach here is?</p>
  
  <p>Thanks!</p>