ASP.NET 成员提供程序无法 return LINQ 查询中的用户角色
ASP.NET Membership provider unable to return user roles in LINQ query
我正在尝试使用 LINQ 获取所有用户及其角色的列表。
该应用程序基于 Membership Provider ver.1,我想我可以从下面的配置行中看到:
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="2" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<add connectionStringName="DefaultConnection" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
我的 LINQ 查询是这样的:
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = Roles.GetRolesForUser(u.UserName) //doesn't work
}).ToList();
问题是 Roles = Roles.GetRolesForUser(u.UserName) 不起作用,因为 class Roles 没有没有任何方法 GetRolesForUser()
我什至无法使用 UsersInRoles 进行查询,因为此 table 未映射,如下所示。
我以前见过很多,我知道当你想使用 LINQ
获取数据并使用另一个 LINQ
时,它不起作用。
解决此问题的最佳方法是,您必须在此 Query
之前获取所有 Roles
(当您的数据保存在内存中时,您可以轻松获取这些数据)或使Awaitable
的方法如下:
带内存:
var allRoles = _roleMaganer.GetAllRoles();
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = allRoles.FirstOrDefault(x=>x.UserName == u.UserName)
}).ToList();
等待:
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = await Roles.GetRolesForUser(u.UserName)
}).ToList();
When the GetRolesForUser
was Async
or MultyTask
when your base Query
want to execute, and you don't used of await
key, exception occured, because the EF can't make other a connection in one Query
我正在尝试使用 LINQ 获取所有用户及其角色的列表。 该应用程序基于 Membership Provider ver.1,我想我可以从下面的配置行中看到:
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="2" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
<providers>
<add connectionStringName="DefaultConnection" applicationName="/" name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</providers>
</roleManager>
我的 LINQ 查询是这样的:
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = Roles.GetRolesForUser(u.UserName) //doesn't work
}).ToList();
问题是 Roles = Roles.GetRolesForUser(u.UserName) 不起作用,因为 class Roles 没有没有任何方法 GetRolesForUser()
我什至无法使用 UsersInRoles 进行查询,因为此 table 未映射,如下所示。
我以前见过很多,我知道当你想使用 LINQ
获取数据并使用另一个 LINQ
时,它不起作用。
解决此问题的最佳方法是,您必须在此 Query
之前获取所有 Roles
(当您的数据保存在内存中时,您可以轻松获取这些数据)或使Awaitable
的方法如下:
带内存:
var allRoles = _roleMaganer.GetAllRoles();
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = allRoles.FirstOrDefault(x=>x.UserName == u.UserName)
}).ToList();
等待:
var users = (from u in db.Users
join m in db.Memberships on u.UserId equals m.UserId
select new
{
m.Email,
u.UserName,
u.UserId,
m.IsLockedOut,
Roles = await Roles.GetRolesForUser(u.UserName)
}).ToList();
When the
GetRolesForUser
wasAsync
orMultyTask
when your baseQuery
want to execute, and you don't used ofawait
key, exception occured, because the EF can't make other a connection in oneQuery