EF1 navigation properties not working?
My entity model was generated from the existing database. There is a many-to-many junction table picked up and hidden by EF.
The relationship is definitely working because this query returns 2 users as expected.
public IQueryable<User> FindUsersByGroupID(int group_id)
{
return db.Users.Where(u => 开发者_Go百科u.Groups.Any(g => g.Group_ID == group_id));
}
But when locating a user that is part of the above result set the Groups navigation property count is 0. I shouldn't have to explicitly join.. right?
public User FindUserByID(int id)
{
return db.Users.First(u => u.User_ID == id);
}
try
db.Users.Include("Groups").First(u => u.User_ID == id);
or load it after with
if (!user.Groups.IsLoaded)
{
user.Groups.Load();
}
精彩评论