开发者

EF1 navigation properties not working?

开发者 https://www.devze.com 2023-01-25 09:23 出处:网络
My entity model was generated from the existing database. There is a many-to-many junction table picked up and hidden by EF.

My entity model was generated from the existing database. There is a many-to-many junction table picked up and hidden by EF.

EF1 navigation properties not working?

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);
    }

EF1 navigation properties not working?


try

db.Users.Include("Groups").First(u => u.User_ID == id);

or load it after with

if (!user.Groups.IsLoaded)
{
    user.Groups.Load();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消