Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6: Foreign keys are not recognized on model generation for Sql Server DB

I'm getting started with Entity Framework and I'm trying to generate a model based on an already existing database. Most of the tables have foreign key references to other tables on unique identifiers that do not happen to be the primary key of the table.

It seems like the model generation is completely ignoring the FK references. The error list shows the following:

Error 6035: The relationship 'My foreign key constraint' has columns that are not part of the key of the table on the primary side of the relationship. The relationship was excluded.

Here's a look at the model and error list

Anybody have any ideas? Is it just that EF doesn't like FK constraints on fields that aren't the primary key?

Thanks in advance.

like image 889
Vigil Avatar asked Feb 03 '14 22:02

Vigil


People also ask

How do you create a foreign key for a model?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship.

Why foreign key is not recommended?

Having active foreign keys on tables improves data quality but hurts performance of insert, update and delete operations. Before those tasks database needs to check if it doesn't violate data integrity. This is a reason why some architects and DBAs give up on foreign keys at all.

Does foreign key update automatically?

Save this answer. Show activity on this post. No the foreign key is not updated automatically. You need to update the foreign key in the tables in which it is referenced by yourself else it would result in referential integrity exception.


1 Answers

Entity framework does not support unique constraints which unfortunately leaves the PK as the only candidate key.

like image 171
Moho Avatar answered Oct 01 '22 11:10

Moho