简体   繁体   中英

How to create a one to many relationship without primary key in SQL Server or Entity Framework?

I have 2 tables as shown here:

Table A :

 [ID] [bigint] IDENTITY(1,1) NOT NULL,
 [AName] [uniqueidentifier] NOT NULL

Table 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. The A.AName column is NOT the primary key.

There can be many rows in TABLE A with the same AName value, but only one row in TABLE B with the ANameID value.

How can I implement this either in SQL or in EF?

If you put a Unique Index on RoadId in table Roads then you can create a Foreign Key.

However, two thoughts:

  1. Why not put a PK on Roads? 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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