尝试在表格中插入值时,我收到以下错误:
INSERT语句与FOREIGN KEY约束冲突" FK_BookingCustomer_Booking"。冲突发生在数据库" C:\ USERS \ B00711882 \ SOURCE \ WORKSPACES \ SAILAWAY \ SAILAWAY FINAL \ SAILAWAYV2 \ SAILAWAYV2 \ APP_DATA \ SAILAWAY.MDF",table" dbo.Booking",栏' ID'。声明已经终止。
我的插入声明:
CREATE PROCEDURE [dbo].[inserBookingCustomer]
@customerID INT,
@bookingID INT
AS
INSERT INTO BookingCustomer (CustomerID, BookingID)
VALUES (@customerID, @bookingID)
RETURN 0
表定义:
Booking
:
CREATE TABLE [dbo].[Booking]
(
[ID] INT IDENTITY (1, 1) NOT NULL,
[CharterID] INT NOT NULL,
[TotalCost] MONEY NOT NULL,
[StartDate] DATE NOT NULL,
[EndDate] DATE NOT NULL,
[TotalDays] INT NOT NULL,
CONSTRAINT [PK_Booking]
PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Booking_CharterID]
FOREIGN KEY ([CharterID]) REFERENCES [dbo].[Charter] ([ID])
);
BookingCustomer
(应插入信息):
CREATE TABLE [dbo].[BookingCustomer]
(
[ID] INT IDENTITY (1, 1) NOT NULL,
[BookingID] INT NOT NULL,
[CustomerID] INT NOT NULL,
CONSTRAINT [PK_BookingCustomer]
PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_BookingCustomer_Booking]
FOREIGN KEY ([BookingID]) REFERENCES [dbo].[Booking] ([ID]),
CONSTRAINT [FK_BookingCustomer_Customer]
FOREIGN KEY ([CustomerID]) REFERENCES [dbo].[Customer] ([CustomerID])
);
这是一个ASP.Net应用程序。
修改:我的c#代码:https://pastebin.com/5a8seRh4
答案 0 :(得分:1)
这是因为您尝试在Booking表中插入以前未插入的值。
您需要先插入本书,然后在下一页插入本书的参考资料