I have three related entities, best described by this diagram:
When I load a user, using the following code, the User
object is loaded, i.e. not null, but the Role
and Department
properties on the User
object are null, despite User database record having valid FK values for these associations.
using (var productionEntities = new ProductionEntities())
{
var userName = GetUserName();
User u = (from usr in productionEntities.UserSet
where usr.UserName == userName
select usr).FirstOrDefault();
if (u == null)
{
throw new KpiSecurityException("No local database entry found for user.") { UserName = userName };
}
return u.Department.DeptName;
}
ASIDE: I went through this whole mission just because the ASP.NET MembershipProvider doesn't support user 'metadata' like department, and I would have had to use the ProfileProvider as well, at the cost of immense DB bloat.