Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF does not generate foreign key association from foreign key in table [duplicate]

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!

like image 909
rsteckly Avatar asked Nov 10 '10 01:11

rsteckly


People also ask

Are duplicates allowed in foreign key?

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.

Can we insert duplicate values in foreign key column?

YES, FOREIGN KEY column can contain duplicate values. FOREIGN KEY can be used for One to Many Relationship.

Can one attribute have two foreign keys?

Foreign Key ColumnsA single column can have multiple foreign key constraints.

How do you set a foreign key in EF?

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.


1 Answers

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.

like image 108
rsteckly Avatar answered Sep 22 '22 01:09

rsteckly