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.
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.
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.
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.
Entity framework does not support unique constraints which unfortunately leaves the PK as the only candidate key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With