Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework foreign keys to non-primary key fields

Is it possible for Entity Framework 4.0 to have an association/navigation property based off of a foreign key to a non-primary key field (it has a unique constraint).

like image 781
devlife Avatar asked Feb 09 '12 19:02

devlife


People also ask

Can foreign key point to a non primary key?

A foreign key can refer to either a unique or a primary key of the parent table. If the foreign key refers to a non-primary unique key, you must specify the column names of the key explicitly.

Does Entity Framework support foreign keys?

Relationships in an Entity Framework Core (EF Core) model are represented using foreign keys (FKs). An FK consists of one or more properties on the dependent or child entity in the relationship.

Can we have table without primary key in Entity Framework?

The Entity framework will not support to have a table without primary key, but we can overcome this issue by accessing the table with additional column via a view and marking the new column as Primary in entity framework. Entity Framework requires primary keys for entities.

What is the difference between primary key and Foreignkey?

A primary key generally focuses on the uniqueness of the table. It assures the value in the specific column is unique. A foreign key is generally used to build a relationship between the two tables. Table allows only one primary key.


1 Answers

No because EF don't understand unique constraint yet and relations in EF must follow same rules as relations in database. Without unique principal relation cannot exist and the only way to get unique principal in EF is using primary key.

like image 141
Ladislav Mrnka Avatar answered Oct 07 '22 14:10

Ladislav Mrnka