Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ to SQL SubmitChanges() Inserts two Database Rows and one Child Row
    primarykey
    data
    text
    <p>I have this going me crazy,</p> <p>I'm attaching a List with 1 Customer and 1 Address child record row. Everything seems OK while debugging. 1 customer Row and 1 Address Row should inserted. But instead I get 2 Customer Records and 1 Address Row.</p> <p>I don't know why. When Attaching and looping inside the List only 1 record seen.</p> <p>Any points?</p> <p>[EDITED]</p> <p>Code Attached:</p> <pre><code>public bool InsertUpdateCustomers(List&lt;Customer&gt; customerList, List&lt;Customer&gt; originalCustomers) { using (DbContext db = new DbContext(DbContext.ConnectionString)) { db.Log = Console.Out; List&lt;Customer&gt; customerCloned = new List&lt;Customer&gt;(); customerList.ForEach(p =&gt; customerCloned.Add(p.CloneObjectGraph())); customerCloned.ForEach(p =&gt; p.Address = customerList.Where(pe =&gt; pe.Id == p.Id).Single().Address.CloneObjectGraph()); customerCloned.ForEach(p =&gt; { if (p.Id &gt; 0) { db.Customer.Attach(p, originalCustomers.Single( x =&gt; x.Id == p.Id)); db.Address.Attach(p.Address, originalCustomers.Single( x =&gt; p.AddressId== x.AddressId). Address); } }); customerCloned.ForEach(p =&gt; { if (p.Id == 0) db.Customer.InsertOnSubmit(p); }); try { db.SubmitChanges(ConflictMode.ContinueOnConflict); return true; } catch (Exception ex) { return false; } } } </code></pre> <p>I have checked the Log in the output and I see indeed 2 Inserts in the table. I don't see nothing about the Address, but inserts correctly.</p> <p>It could be the foreign key problem i don't get it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload