I have a table tblEvent and another table tblContact. There is a field ContactID in tblEvent that is linked by a foreign key to ContactID in tblContact. I've triple checked; the foreign key is in the database.
The Entity Framework model DOES NOT generate a navigation property and foreign key association for it as it does for the other foreign keys.
What could be different about this foreign key that EF does not generate a navigation property for it?
Thanks!
By default there are no constraints on the foreign key, foreign key can be null and duplicate. while creating a table / altering the table, if you add any constrain of uniqueness or not null then only it will not allow the null/ duplicate values.
YES, FOREIGN KEY column can contain duplicate values. FOREIGN KEY can be used for One to Many Relationship.
Foreign Key ColumnsA single column can have multiple foreign key constraints.
The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.
I found the solution here:
Why doesn't EF 4 generate association for FK relation to column with unique index?
The reason it was not recognizing it is was because the table had a unique non-clustered index on the foreign key field.
Apparently, a unique, non-clustered index allows for a null value which can't be mapped.
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