How to use foreign key in L2E or EF?
I have a table User
that has a PrivilegeId
foreign key, points to the table Privilege
and is the primary key.
In Entity Framework, VS will not generate a variable PrivilegeId
in User
for you. It will generate property Privilege
and property PrivilegeReference
for you.
When I load User
, the property Privilege
is null
default. This means that EF does not automatically load the referenced object for you. Think I might be doing something wrong? I cannot download Privilege
separately because at that time I have no information about Privilege
. I think EF is supposed to download links for me, but I am missing something. I need PrivilegeId
one associated with my object User
.
Can anyone help me?
EDIT:
another answer: What are navigation properties in Entity Framework for?
a source to share
It sounds like you want to Include method .
List<User> users = context.Users.Include("Privilege").ToList();
a source to share