title: Error when adding new entity to EF : A referential integrity constraint violation occurred
body: <p>I'm adding a new entity that is built through an external class and I get this error :  <code>A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.</code> : </p>
  
  <p><strong>Edit</strong> : The <code>ReconFact</code> entity is not linked to any other entities</p>
  
  <p>The code (<strong>edited</strong>) :</p>
  
  <pre><code>// the context class
  AccountingModelContext db = new AccountingModelContext();
  // query some data
  List&lt;Reconciliation&gt; recon = db.Reconciliations
          .Where(r =&gt; r.ReconNum == 112293)  // scenario 18 
          .Include(r =&gt; r.ReconciliationDetails.Select(rd =&gt; rd.JrnlEntryDetail).Select(jd =&gt; jd.JrnlEntry).Select(j =&gt; j.ARInvoices.Select(i =&gt; i.ARInvoiceDetails)))
          .Include(r =&gt; r.ReconciliationDetails.Select(rd =&gt; rd.JrnlEntryDetail).Select(jd =&gt; jd.JrnlEntry).Select(j =&gt; j.ARCredMemoes.Select(c =&gt; c.ARCredMemoDetails)))
          .ToList();
  // class that will manager business logic and generate facts with above data
  ReconFactGenerator generator = new ReconFactGenerator(recon);
  // call to the method that will return a list of facts
  List&lt;ReconFact&gt; scenario18 = generator.GenerateFacts();
  // ERROR RAISED HERE
  scenario18.ForEach(f =&gt; db.ReconFacts.Add(f));
  // never reached
  db.SaveChanges();
  </code></pre>
  
  <p>It's very basic I build a list of <code>ReconFact</code>entities with <code>ReconFactGenerator</code>class. No ID's are specified as they are new entities. </p>
  
  <p><strong>edited</strong>
  It works perfectly if I create a new entity in the same method like here Before I query data from the context :</p>
  
  <pre><code>ReconFact testfact = new ReconFact { ItemCode = "test", ReconNum = 1234, ReconDate = DateTime.Parse("2099-01-01"), ShortName = "L_SZTREDFD", InvoiceAmount = 0, CredMemoAmount = 0, IncomingPayAmount = 0, OutgoingPayAmount = 0, JrnlEntryAmount = 0, OtherAmount = 0 };
  db.ReconFacts.Add(testfact); // no problems no error raised
  db.SaveChanges(); // I can see the new record in my database 
                    // the ID was generated successfully 
                    // as I've specified identity(1,1) in the database
  </code></pre>
  
  <p><strong>Added Edit 2</strong> 
  After I query data from the context like this : </p>
  
  <pre><code> List&lt;Reconciliation&gt; recon = db.Reconciliations
          .Where(r =&gt; r.ReconNum == 112293)  // scenario 18 
          .Include(r =&gt; r.ReconciliationDetails.Select(rd =&gt; rd.JrnlEntryDetail).Select(jd =&gt; jd.JrnlEntry).Select(j =&gt; j.ARInvoices.Select(i =&gt; i.ARInvoiceDetails)))
          .Include(r =&gt; r.ReconciliationDetails.Select(rd =&gt; rd.JrnlEntryDetail).Select(jd =&gt; jd.JrnlEntry).Select(j =&gt; j.ARCredMemoes.Select(c =&gt; c.ARCredMemoDetails)))
          .ToList();
  </code></pre>
  
  <p>The context is not able to save anymore returning the above error. </p>
  
  <p><strong>edited</strong> Why is an error raised when I want to save after I query the context and raises no error before ?</p>