[英]How to create a one to many relationship without primary key in SQL Server or Entity Framework?
I have 2 tables as shown here:我有 2 个表,如下所示:
Table A
:表
A
:
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[AName] [uniqueidentifier] NOT NULL
Table B
:表
B
:
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[ANameID] [uniqueidentifier] UNIQUE NOT NULL
I want to create a one-to-many relationship between B
and A
where the ANameID
in the B
table can reference one or more rows in the A
table using the AName
column in the A
table.我想在
B
和A
之间创建一对多的关系,其中B
表中的ANameID
可以使用A
表中的AName
列引用A
表中的一个或多个行。 The A.AName
column is NOT the primary key. A.AName
列不是主键。
There can be many rows in TABLE A with the same AName value, but only one row in TABLE B with the ANameID value.表 A 中可以有许多行具有相同的 AName 值,但表 B 中只有一行具有 ANameID 值。
How can I implement this either in SQL or in EF?如何在 SQL 或 EF 中实现此功能?
If you put a Unique Index on RoadId in table Roads then you can create a Foreign Key.如果在 Roads 表中的 RoadId 上放置唯一索引,则可以创建外键。
However, two thoughts:但是,有两个想法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.