Inserting Rows in Relationship using a Strongly Typed DataSet

Posted by Manuel Faux on Stack Overflow See other posts from Stack Overflow or by Manuel Faux
Published on 2009-08-25T12:51:40Z Indexed on 2010/05/08 15:58 UTC
Read the original article Hit count: 361

I'm using ADO.NET with a strongly typed dataset in C# (.NET 3.5). I want to insert a new row to two tables which are related in an 1:n relation.

The table Attachments holds the primary key part of the relation and the table LicenseAttachments holds the foreign key part.

AttachmentsDataSet.InvoiceRow invoice; // Set to a valid row, also referenced in InvoiceAttachments
AttachmentsDataSet.AttachmentsRow attachment;
attachment = attachmentsDataSet.Attachments.AddAttachmentsRow("Name", "Description");
attachmentsDataSet.InvoiceAttachments.AddInvoiceAttachmentsRow(invoice, attachment);

Of course when I first update the InvoicesAttachments table, I'll get a foreign key violation from the SQL server, so I tried updating the Attachments table first, which will create the rows, but will remove the attachment association in the InvoiceAttachments table. Why?

How do I solve this problem?

© Stack Overflow or respective owner

Related posts about strongly-typed-dataset

Related posts about .NET