我想:如何从LINQ表达式获取数据库中某个角色的所有ApplicationUser列表?
- 的
ApplicationUsers
谁在角色"NormalUser"
任何人 - 所有
ApplicationUsers
只只管理员列表清单。
我这样做:
// GET: ApplicationUsers
public ActionResult Index() {
// if you are Admin you will get all users
if (User.IsInRole("Admin"))
return View(db.Users.ToList());
//if you are somebody else(not Admin) you will see only list of NormalUsers
//HERE I GET ERROR
var list = db.Users.Where(x => UserManager.IsInRole(x.Id, "NormalUser")).ToList(); // here I get error
return View(list);
}
的UserManager内部代码上面是:UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
但不幸的是我的LINQ expresiion不正确。我得到错误:
LINQ to Entities does not recognize the method 'Boolean IsInRole[ApplicationUser,String](Microsoft.AspNet.Identity.UserManager`2[WebApplication2.Models.ApplicationUser,System.String], System.String, System.String)' method, and this method cannot be translated into a store expression.
问题:如何正确地了解谁是角色"NormalUser"
用户列表?
角色如何存储在应用程序用户中?它是每个用户的一个角色,还是一个用户可以属于多个角色?我会添加一个回答,看看它是如何为你做的。 – 2014-09-24 12:54:25