我有一个简单的控制台应用程序,我正在尝试更新客户和订单表,我收到一条错误消息:
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.
我错过了一些明显的东西吗?
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);
答案 0 :(得分:1)
无论问题出在C#端,都要使用Sql Profiler来检查在SQL Server端执行的sql语句的顺序。这样您就可以快速找到问题。
可能UpdateAll方法以无效顺序执行两次插入。使用表适配器而不是ORMapper,您会问自己这些问题。