I have used to create a custom role provider.
My Project - asp.net MVC3, Entity Framework 4
There is a problem with the GetRolesForUser method.
Custom RoleProvider - GetRolesForUser Method
// The user to return a list of roles for.
public override string[] GetRolesForUser(string userAccount)
{
var userId = userRepository.GetUser(userAccount).UserId;
var roleIds = from ur in usersInRoleRepository.UsersInRoles
where ur.UserId == userId
select ur.RoleId;
var roleNames = from r in roleRepository.Roles
where roleIds.Contains(r.RoleId)
select r.RoleName;
if (roleNames != null)
{
**return roleNames.ToArray(); // here Error**
}
else
{
return new string[0];
}
}
Error Message : The specified LINQ expression contains references to queries that are associated with different contexts.
I do not know whether there is something wrong with this code.
Please Help.
User Entity
public class User
{
public Guid UserId { get; set; }
public string UserAccount { get; set; }
public string Password { get; set; }
public string UserEmail { get; set; }
public DateTime JoinDate { get; set; }
public DateTime LoginDate { get; set; }
}
Role Entity
public class Role
{
public Guid RoleId { get; set; }
public string RoleName { get; set; }
public DateTime CreateDate { get; set; }
}
UsersInRole Entity
public class UsersInRole
{
public Guid UserId { get; set; }
public Guid RoleId { get; set; }
public DateTime SetDate { get; set; }
}
You cant make queries that are associated with different contexts in single query. Try to change your roleIds linq code to avoide it's lazy loading by converting it to array:
var roleIds = (from ur in usersInRoleRepository.UsersInRoles
where ur.UserId == userId
select ur.RoleId).ToArray();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.