简体   繁体   English

无法通过edmx和LinQ中的Include获得相关表

[英]cannot get related table with Include in edmx and LinQ

Now I have 3 tables: 现在我有3张桌子:

tbl_User: UserID, UserName tbl_User:用户ID,用户名

tbl_Role: RoleID, RoleName tbl_Role:RoleID,RoleName

tbl_UserRole: UserID, RoleID tbl_UserRole:用户ID,角色ID

The relationship is: tbl_User - tbl_UserRole - tbl_Role (1 User for many Role, and 1 Role for many User) 关系为:tbl_User-tbl_UserRole-tbl_Role(1个用户代表多个角色,1个角色代表多个用户)

in edmx, I tried to use like this: 在edmx中,我试图这样使用:

var users =
    ((from u in DbContext.tbl_User
    join ur in DbContext.tbl_UserRole on u.UserID equals ur.UserID
    join r in DbContext.tbl_Role on ur.RoleID equals r.RoleID
    select u) as ObjectQuery<tbl_User>).Include("tbl_UserRole").Include("tbl_Role");

What I need to get information of tbl_User and RoleName (in tbl_Role). 我需要获取tbl_User和RoleName信息(在tbl_Role中)。 How can I do that? 我怎样才能做到这一点?

Please advise. 请指教。 Thanks. 谢谢。

If you're simply just trying to get a users with their roles, can you not just: 如果您只是想让用户扮演他们的角色,那么您不仅可以:

var users = DbContext.tbl_User.Include("tbl_Role")

Without a clearer picture of your entity relationships, I'm going to assume that this will work. 如果没有更清晰地了解您的实体关系,我将假定这将起作用。 If you were to show the entity classes or a picture of your EDMX designer then a more relevant answer might be posted 如果要显示实体类或EDMX设计器的图片,则可能会发布更相关的答案

Edit: Assuming the above code fetched you your users, you could access the roles as follows: 编辑:假设上面的代码提取了您的用户,则可以按以下方式访问角色:

users.First().tbl_role.Select(x => x.RoleName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM