views:

521

answers:

1

I wasted the better part of the day on this and am no closer to a understanding the issue than what I was this morning.

I am looping through a set of objects and marking them for deletion. The 2nd one always causes the above exception. tb_invoice has a FK to tb_shipment.

As always, I am probably missing something very obvious, but I have stripped out so much from this code already that there is nothing left, and I am still getting this exception. This is a local SQL 2008 instance and there is of course nothing and nobody changing the invoice in between reading them and calling SubmitChanges(). Help!

myDataContext db = new myDataContext(); 

IQueryable<invoiceDetail> pendingInvoices  
   = db.GetInvoiceDetailPending(); 

foreach (invoiceDetail id in pendingInvoices) { 
    tb_shipment s = db.GetShipmentById((Guid)id.shipment_id); 

    db.tb_invoices.DeleteOnSubmit(
          db.GetInvoiceById(s.tb_invoices.FirstOrDefault().id));        } 

    SubmitChanges();    // fails for the 2nd invoice 
} 
A: 

http://stackoverflow.com/questions/805968/system-data-linq-changeconflictexception-row-not-found-or-changed/1087029

Setting the Delete action for the foreign keys in SQL Server to "Cascade" did the same. I wish I had remembered earlier that I ran into this before.

cdonner