简体   繁体   English

如何在 SQL 服务器或实体框架中创建没有主键的一对多关系?

[英]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.我想在BA之间创建一对多的关系,其中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:但是,有两个想法:

  1. Why not put a PK on Roads?为什么不在道路上进行PK? Could be RoadId...可能是路牌...
  2. Given that an intersection should a minimum of two things, your design is not complete as presented.鉴于交叉点至少应包含两件事,因此您的设计并不完整。 I assume that this is on purpose, but just thought I'd mention it...我认为这是故意的,但只是想我会提到它......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM