Lets say you have two entity named Parent and Child. Child entity is DEPENDENT of Parent entity. A weak key of child entity is the NAMEOFCHILD. Is it possible for the Parent entity to have NAMEOFCHILD as a foreign key?

This idea has not been talked about in class. I was wondering is this possible in SQL? If so, should i just add

 FOREIGN KEY (NAMEOFCHILD) source CHILD 

in my table?

有帮助吗?

解决方案

In the database schema, yes (if Child.NAMEOFCHILD has a unique index). In entity framework, no. EF doesn't support associations to unique indexes (yet). But this is just on the technical level. Whether it's meaningful is another question.

Also, beware of painting yourself in a corner. When both foreign keys are not nullable you'd never be able to insert data, because you can't insert two records at a time and sequential inserts always cause foreign key violations. You would be able to design the database schema but never get any data in.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top