我正在构建一个MVC 5(ef6)应用程序,该应用程序具有链接到用户表的摄影师表。
我通过在用户帐户上设置角色来确定摄影师。摄影师表拥有其他信息而不是表。我有不同类型的用户访问系统。
我需要根据用户在摄影师角色中获得摄影师表记录。我有以下解决方案,但是当我尝试访问查询结果时,它会带有以下错误消息。
无法创建类型的'Microsoft.aspnet.Identity.entityFramework.IdentityUserrole'的常数值。在这种情况下仅支持原始类型或枚举类型。
var photogpraherRole = db.Roles.Single(r => r.Name == "photographer");
var users = photogpraherRole.Users.ToList();
var photographersList =
from photographer in db.Photographers
join user in users on photographer.User.Id equals user.UserId
orderby photographer.Priority
select photographer;
foreach (var photographer in photographersList)
{
var photographerName = photographer.User.FirstName;
}
在进行了一些研究和@shawn yan的帮助之后,这是我提出的解决方案。
var photographerRole = db.Roles.SingleOrDefault(m => m.Name == "photographer");
var disabledRole = db.Roles.SingleOrDefault(m => m.Name == "disabled");
var users = db.Users.Where(m => m.Roles.All(r => r.RoleId == photographerRole.Id && r.RoleId != disabledRole.Id)).ToList();
var photographersList =
from photographer in db.Photographers.ToList()
join user in users on photographer.User.Id equals user.Id
where photographer.Location == booking.location.Location
orderby photographer.Priority
select photographer;
var photographers = photographersList.ToList();