EF - 导航属性解释

发布于 2024-10-15 09:32:17 字数 1355 浏览 2 评论 0原文

我使用 EF 4 和随 ASP.Net 4.0 一起提供的会员提供程序。

我需要查找特定角色中的所有用户并使用 List 填充 DropDownList。

涉及的数据库表有:

aspnet_Roles
aspnet_Users
aspnet_UsersInRoles

从 aspnet_Users 到 aspnet_Roles 的导航属性(使用联结表 aspnet_UsersInRoles)是:

myUser.aspnet_Roles

匹配 RoleID(Guid)“CE44ED48-E9F9-49C6-9E15-E40EEFDC7479”)

我的脚本:

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

问题: IF 总是返回 FALSE,所以我无法填充 List<>。 我想我在某些属性上做错了。

你有什么想法吗?感谢您抽出时间。

I use EF 4 and Membership Provider Shipped with ASP.Net 4.0.

I need find all Users in a Specific Role and Populate a DropDownList using List<ListItem>.

DataBase Tables involved are:

aspnet_Roles
aspnet_Users
aspnet_UsersInRoles

Navigation Property from aspnet_Users to aspnet_Roles (using junction table aspnet_UsersInRoles) is:

myUser.aspnet_Roles

Matching RoleID (Guid) "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<>.
I suppose I am doing wrong in if an some Properties.

Do you have any ideas? Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-10-22 09:32:17

myUser.aspnet_Roles 是一个 EntitySet(换句话说,是角色的集合,因此它的字符串表示形式不太可能发生在匹配您要查找的角色 ID 的字符串表示形式。

有多种方法可以解决此问题:

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

另请注意,from y in x select yx 相同。,这样你就可以编写

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

并且不必费心声明userQueryroleQuery(你实际上似乎没有在任何地方使用roleQuery。无论如何...)

另外,您可以考虑使用会员资格/角色 API 来获取角色中的用户:

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

请注意,这会返回用户名数组,而不是用户对象,因此您必须做一些额外的工作才能获得用户 ID,但这是你可以考虑的事情......

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.

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

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

And not bother declaring userQuery or roleQuery. (You don't actually seem to be using roleQuery anywhere anyway...)

Also, you could look at using the membership/roles API to fetch users in a role:

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文