EF - 导航属性解释
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
myUser.aspnet_Roles
是一个EntitySet
(换句话说,是角色的集合,因此它的字符串表示形式不太可能发生在匹配您要查找的角色 ID 的字符串表示形式。有多种方法可以解决此问题:
另请注意,
from y in x select y
与x 相同。
,这样你就可以编写并且不必费心声明
userQuery
或roleQuery
(你实际上似乎没有在任何地方使用roleQuery
。无论如何...)另外,您可以考虑使用会员资格/角色 API 来获取角色中的用户:
请注意,这会返回用户名数组,而不是用户对象,因此您必须做一些额外的工作才能获得用户 ID,但这是你可以考虑的事情......
myUser.aspnet_Roles
is anEntitySet<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:
Also note that
from y in x select y
is just the same asx
, so you could writeAnd not bother declaring
userQuery
orroleQuery
. (You don't actually seem to be usingroleQuery
anywhere anyway...)Also, you could look at using the membership/roles API to fetch users in a role:
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...