简体   繁体   English

EF-导航属性说明

[英]EF - Navigation property explanation

I use EF 4 and Membership Provider Shipped with ASP.Net 4.0. 我使用EF 4和ASP.Net 4.0附带的成员资格提供程序。

I need find all Users in a Specific Role and Populate a DropDownList using List<ListItem> . 我需要找到所有具有特定角色的用户,并使用List<ListItem>填充DropDownList。

DataBase Tables involved are: 涉及的数据库表是:

aspnet_Roles
aspnet_Users
aspnet_UsersInRoles

Navigation Property from aspnet_Users to aspnet_Roles (using junction table aspnet_UsersInRoles) is: 从aspnet_Users到aspnet_Roles的导航属性(使用联结表aspnet_UsersInRoles)是:

myUser.aspnet_Roles

Matching RoleID (Guid) "CE44ED48-E9F9-49C6-9E15-E40EEFDC7479") 匹配的RoleID(向导)“ CE44ED48-E9F9-49C6-9E15-E40EEFDC7479”)

My Script: 我的剧本:

        using (CmsConnectionStringEntityDataModel context = new CmsConnectionStringEntityDataModel())
        {
            IQueryable<aspnet_Users> userQuery = from aspnet_Users in context.aspnet_Users select aspnet_Users;
            IQueryable<aspnet_Roles> roleQuery = from aspnet_Roles in context.aspnet_Roles select aspnet_Roles;
            List<ListItem> myListUsersInRoles = new List<ListItem>();
            foreach (aspnet_Users myUser in userQuery)
            {
            // PROBLEM HERE
            if (myUser.aspnet_Roles.ToString() == "CE44ED48-E9F9-49C6-9E15-E40EEFDC7479")
                myListUsersInRoles.Add(new ListItem(myUser.UserName.ToString(), myUser.UserId.ToString()));

            uxListUsers.DataSource = myListUsersInRoles;
            uxListUsers.DataBind();
        }`

Problems: IF return FALSE always, so I am not able to populate List<>. 问题:如果始终返回FALSE,则无法填充List <>。 I suppose I am doing wrong in if an some Properties. 我想我在某些属性方面做错了。

Do you have any ideas? 你有什么想法? Thanks for your time. 谢谢你的时间。

myUser.aspnet_Roles is an EntitySet<aspnet_Roles> (in other words, a collection of roles, so it's highly unlikely that it's string representation happens to match the string representation of the role you're after's ID. myUser.aspnet_RolesEntitySet<aspnet_Roles> (换句话说,是角色的集合 ,因此,它的字符串表示形式不太可能与您要获得ID的角色的字符串表示形式相匹配。

There are several ways you could fix it. 有几种方法可以修复它。 One is: 一种是:

// ...
if (myUser.aspnet_Roles.Any(r => r.RoleId.ToString() == "CE44ED48-E9F9-49C6-9E15-E40EEFDC7479"))
// ...

Also note that from y in x select y is just the same as x , so you could write 另请注意, from y in x select yx相同,因此您可以编写

foreach (aspnet_User myUser in context.aspnet_Users)
// ...

And not bother declaring userQuery or roleQuery . 而且不必费心声明userQueryroleQuery (You don't actually seem to be using roleQuery anywhere anyway...) (实际上,您似乎根本没有在任何地方使用roleQuery ……)

Also, you could look at using the membership/roles API to fetch users in a role: 另外,您可以看看使用会员资格/角色API来获取角色中的用户:

using System.Web.Security; // at the top of your file
var usersInRole = Roles.GetUsersInRole("name of role here, NOT the guid");

Note that this returns an array of usernames, not user objects, so you'd have to do a little extra work to get the User ID, but it is something you could think about... 请注意,这将返回用户名数组,而不是用户对象,因此您需要做一些额外的工作来获取用户ID,但这是您可以考虑的...

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

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