0

In table A I've got a composite of 3 columns as a primary key. I want to have only one of these three columns as a foreign key in table B, just to make sure that the value that I insert into table B's column exists in table A.

Currently from what I've read it looks like in Entity Framework I have to add all three columns of composite PK, which is not really what I need. The latest answer that I've found was of 2015, maybe since then something changed?

I know that I can add a manual check on each insert/update call, but I don't want to do that, maybe there is more elegant way.

jbl
  • 15,179
  • 3
  • 34
  • 101
Dzhara
  • 89
  • 1
  • 9
  • Nothing has changed. Why? Because you can't create such FK relationship in RDBMS (except if the column is unique, but then composite PK makes no sense). – Ivan Stoev Oct 09 '17 at 08:08
  • If the column is not declared PK or UNIQUE NOT NULL in the referenced table then [the constraint you want is not a FK constraint](https://stackoverflow.com/a/46467006/3404097). You must typically enforce it by triggers or change your design so that the referenced column is a PK/UNN in a table. Also it can be that your design is poor and an appropriate design would allow the constraint naturally as a FK. Give more details--a [mcve]--and confirm *all* restrictions that must be enforced on the collected relevant tables. – philipxy Oct 09 '17 at 10:24
  • Yep, I will probably go with adding a trigger which will check what I need. Thank you for comments! – Dzhara Oct 09 '17 at 12:20

0 Answers0