tags:

views:

31

answers:

1

I have a User table which has a PrivilegeId foreign key points to a Privilege table and is the primary key there.

In Entity Framework, the VS will not generate a PrivilegeId variable under User for you. It will generate a Privilege property and PrivilegeReference property for you instead.

When I load a User, the Privilege property is null by default. That means EF does not load refered entity for you automatically. I think I may did something wrong? I cannot load Privilege separatedly because I have no info about the Privilege at that time. I guess EF should load the refered entities for me but I missed something. I need the PrivilegeId associated with my User object.

anybody can help me?

EDIT:

another answer: http://stackoverflow.com/questions/1262307/what-are-navigation-properties-in-entity-framework-for

+1  A: 

Sounds like you want the Include method.

List<User> users = context.Users.Include("Privilege").ToList();
David B
is there a way to get the table name programmatically instead of hard code the table name?
5YrsLaterDBA
The method accepts a string, and it's easy to programmatically supply a string.
David B
If instead you want a compiler-checked technique be used instead of a compiler-opaque string, here's a link. http://blogs.msdn.com/stuartleeks/archive/2008/08/27/improving-objectquery-t-include.aspx
David B