0

I'm using Entity Frame Core Code First with an ASP MVC project I'm working on.

I have a table with two foreign keys that reference the same table. I followed this great example: Entity Framework Code First - two Foreign Keys from same table

How can I enforce it so that the value combinations can only exist once?

i.e.

Id   Value1    Value
1    1         2        --This is ok
2    1         3        --This is ok
3    1         2        --This is bad. Duplicate combination
4    3         1        --This is bad. This is just the reverse of Id 2

Thank you.

Community
  • 1
  • 1
RobHurd
  • 2,041
  • 7
  • 26
  • 34
  • 2
    The duplicate `1,2` can be covered by a unique index (`.HasIndex()`), preventing reverse references can only be enforced by validations. – Gert Arnold Aug 21 '16 at 19:48
  • @GertArnold thank you, your direction helped me find a solution to this. https://docs.efproject.net/en/latest/modeling/indexes.html I will need to do more research concerning validation to prevent the reverse from happening. – RobHurd Aug 21 '16 at 22:34

0 Answers0