I’m using VS2008 (.NET 3.5) and Entity Framework over SQL Server. I’m hoping to get the value of a foreign key column without using the Include() method to link up the table.
For example, the simple Author has many Books sample.
var book = context.Books.First();
int authorId = book.Author.AuthorId;
In this scenario, an exception is thrown, because Author is null. I’m looking to just get the author ID, but don’t need the rest of the Author object. There is no AuthorId property on the Book class.
I’m sure there’s a way to do this, probably by editing the EDMX directly, or maybe through the designer – any ideas how to get started?
Thanks
You can get the FK value but it is not as straight forward. Each your navigation property representing single entity should have its pair property called
XXXReference. In your caseAuthorReference. This is the EF infrasturcture property holding your key internally. You can use similar code to get your FK value: