I am using
var s = db.sModels.ToList().First(r => r.Status == CustomStatus.Available);
to fetch the first available item.
However, when fetched, a foreign key in s turns out to be null.
I have connected to the database and saw that the foreign key is not null (but is 1).
Why does it become null?
Assuming this is Linq to Entities – that is the default behavior – if you want to materialize the related entity – let’s say
SomeEntityyou can use anIncludequery in which case the FK and entity will be populated:For a workaround w/o using an Include query to just retrieve the FKs see this blog post: “Getting Foreign Key Values with Linq-to-Entities”
As noted in a comment to the question Linq to Entities does not support enums right now hence above would actually not work as is. As a workaround (until EF 5) you can use integers on the DB for now: