i have a plain and simple console app where i’m trying to update customers and orders tables and i’m getting an error message:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_Customers". The conflict occurred in database "CustomerOrder", table "dbo.Customers", column 'Id'.
The statement has been terminated.
Am I missing something obvious?
RsNorthwinds ds = new RsNorthwinds();
RsNorthwinds.CustomersRow cRow = ds.Customers.NewCustomersRow();
cRow.Name = "John Smith";
ds.Customers.AddCustomersRow(cRow);
RsNorthwinds.OrdersRow oRow = ds.Orders.NewOrdersRow();
oRow.CustomerId = cRow.Id;
oRow.OrderDate = "9/26/11";
ds.Orders.AddOrdersRow(oRow);
RsNorthwindsTableAdapters.TableAdapterManager tm =
new DataTableAdapterTester.RsNorthwindsTableAdapters.TableAdapterManager();
tm.OrdersTableAdapter = new DataTableAdapterTester.RsNorthwindsTableAdapters.OrdersTableAdapter();
tm.CustomersTableAdapter = new DataTableAdapterTester.RsNorthwindsTableAdapters.CustomersTableAdapter();
tm.UpdateAll(ds);
Regardless of that the issue is at the C# side, always use the Sql Profiler to check the sequence of sql statements that is executed at the SQL Server side. This way you’ll quickly locate the issue.
Possibly the UpdateAll method executes both inserts in an invalid order. Using Table Adapters instead of an ORMapper you are asking yourself for such issues.