Reputation: 2014
I found this URL about how to add an item in my table. http://msdn.microsoft.com/en-us/library/bb386941.aspx
// Create a new Order object.
Order ord = new Order
{
OrderID = 12000,
ShipCity = "Seattle",
OrderDate = DateTime.Now
// …
};
This should work without any problems
but if I have something like this:
// Create a new Order object.
Order ord = new Order
{
OrderID = 12000,
CustomerID = 22, // where CustomerID is a foreign key to table Customer
ShipCity = "Seattle",
OrderDate = DateTime.Now
// …
};
Then it will give errors like: Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK7_REVIEW". The conflict occurred in database "Dbname", table "dbo.Customer", column 'CustomerID'. The statement has been terminated.
How can I insert a row in my table, whith foreign keys?
Upvotes: 1
Views: 3616
Reputation: 14648
Order.CustomerID
refers to a primary key in another table (say Customer
). To insert the new order, the CustomerID
must be equal to an existing value of the primary key in Customer
table.
Upvotes: 1