在Visual Studio中定义多个外键

时间:2021-10-27 17:01:27

I have a basic database schema as follows (dumbed down so it's easy to read):

我有一个基本的数据库模式如下(dumbed down,因此很容易阅读):

[Staff]
StaffId (pk)
FirstName
LastName

[RosterEvent]
EventId (pk)
StartDate
EndDate

[StaffEvents]
StaffId (pk)
EventId (pk)

Ok so, many Staff can have many RosterEvents, which is why I added the StaffEvents table. It resolves the many-to-many relationship. However I do not know how to define the foreign keys.

好的,很多员工可以有很多RosterEvent,这就是我添加StaffEvents表的原因。它解决了多对多关系。但是我不知道如何定义外键。

In Visual Studio, how do I define these as foreign keys using the Table Designer? If I try to add the relationship using the Foreign Key Relationships dialog box, I get the error message "The columns in table 'StaffEvents' do not match an existing primary key or UNIQUE constraint". Even though the UNIQUE constraint has been applied to all primary keys on every table.

在Visual Studio中,如何使用表设计器将它们定义为外键?如果我尝试使用“外键关系”对话框添加关系,则会收到错误消息“表中的列'StaffEvents'与现有主键或UNIQUE约束不匹配”。即使UNIQUE约束已应用于每个表上的所有主键。

Help is much appreciated! Thanks!

非常感谢帮助!谢谢!

2 个解决方案

#1


In the table designer you can click the row headers on the side of the two columns you want and click the primary key icon on the toolbar.

在表设计器中,您可以单击所需两列旁边的行标题,然后单击工具栏上的主键图标。

Are your PK actually marked as Primary Keys or just UNIQUE? Are they the same data-type?

您的PK实际上是标记为主键还是仅仅是UNIQUE?它们是相同的数据类型吗?

#2


All primary keys on every table are marked as PK and UNIQUE. They are also all of type int.

每个表上的所有主键都标记为PK和UNIQUE。它们也都是int类型。

This is how I am trying to set up the relationship at the moment.

这就是我此刻建立关系的方式。

  1. From the Staff table I click the 'Relationships' toolbar button.
  2. 在员工表中,我单击“关系”工具栏按钮。

  3. I click 'Add'
  4. 我点击“添加”

  5. I click on the (...) from the 'Tables and Columns Specifications' property.
  6. 我点击'Tables and Columns Specifications'属性中的(...)。

  7. I select 'StaffEvents' as the primary key table.
  8. 我选择'StaffEvents'作为主键表。

  9. I select 'StaffId' from the drop down box.
  10. 我从下拉框中选择'StaffId'。

When I click OK, I get the error message "The columns in table 'StaffEvents' do not match an existing primary key or UNIQUE constraint".

当我单击确定时,我收到错误消息“表中的列'StaffEvents'与现有的主键或UNIQUE约束不匹配”。

#1


In the table designer you can click the row headers on the side of the two columns you want and click the primary key icon on the toolbar.

在表设计器中,您可以单击所需两列旁边的行标题,然后单击工具栏上的主键图标。

Are your PK actually marked as Primary Keys or just UNIQUE? Are they the same data-type?

您的PK实际上是标记为主键还是仅仅是UNIQUE?它们是相同的数据类型吗?

#2


All primary keys on every table are marked as PK and UNIQUE. They are also all of type int.

每个表上的所有主键都标记为PK和UNIQUE。它们也都是int类型。

This is how I am trying to set up the relationship at the moment.

这就是我此刻建立关系的方式。

  1. From the Staff table I click the 'Relationships' toolbar button.
  2. 在员工表中,我单击“关系”工具栏按钮。

  3. I click 'Add'
  4. 我点击“添加”

  5. I click on the (...) from the 'Tables and Columns Specifications' property.
  6. 我点击'Tables and Columns Specifications'属性中的(...)。

  7. I select 'StaffEvents' as the primary key table.
  8. 我选择'StaffEvents'作为主键表。

  9. I select 'StaffId' from the drop down box.
  10. 我从下拉框中选择'StaffId'。

When I click OK, I get the error message "The columns in table 'StaffEvents' do not match an existing primary key or UNIQUE constraint".

当我单击确定时,我收到错误消息“表中的列'StaffEvents'与现有的主键或UNIQUE约束不匹配”。