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!
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.
I know it is an old post, but I was faced with this same problem, I got to this post and rsteckly's comment made me check and indeed I have an index on my main table as non-clutered for my key to improve performance.
A solution I found is to add a filter to the index (WHERE tableId IS NOT NULL), in this way all the references were generated correctly.
Hope this solution will help someone
if you are are using 3.5sp1 that could be the issue.
This article touches on some of the changes and if nothing else you can always manually add the fk association in the entity designer as described in the article.
Link
精彩评论