0

我在预订和联系人之间存在多对多关系,当我尝试将新联系人添加到预订时,会发生以下错误。

    _db.Reservations.Add(reservation);
    reservation.Contacts.Add(user.Contact);
    _db.SaveChanges();

我认为我的表定义存在一些问题,所以我复制了我的表的粘贴创建脚本。我在reservaion_contact 表中同时设置了reservaionId 和contactId 主键。这是错的吗?我的表结构:

CREATE TABLE [dbo].[Reservation_Contacts](
    [Reservation_Id] [int] NOT NULL,
    [Contact_Id] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_Reservation_Contacts] PRIMARY KEY CLUSTERED 
(
    [Reservation_Id] ASC,
    [Contact_Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Reservation_Contacts]  WITH CHECK ADD  CONSTRAINT [FK_Reservation_Contacts_Contact] FOREIGN KEY([Contact_Id])
REFERENCES [dbo].[Contact] ([Id])
GO

ALTER TABLE [dbo].[Reservation_Contacts] CHECK CONSTRAINT [FK_Reservation_Contacts_Contact]
GO

ALTER TABLE [dbo].[Reservation_Contacts]  WITH CHECK ADD  CONSTRAINT [FK_Reservation_Contacts_Reservation] FOREIGN KEY([Reservation_Id])
REFERENCES [dbo].[Reservation] ([Id])
GO

ALTER TABLE [dbo].[Reservation_Contacts] CHECK CONSTRAINT [FK_Reservation_Contacts_Reservation]
GO
4

1 回答 1

2

不要在您的插入中发送contact_id,它是一个身份,它将自动生成。

于 2013-04-01T19:07:50.860 回答