[英]entity framework multiple many to many queries
我有一个简单的安全模型,其中有:
以及这些表之间的多对多链接,因此用户可以使用角色,也可以将角色添加到路径中。 我正在尝试编写一个函数,以便从用户名和路径中返回一个bool值,具体取决于用户是否可以访问该路径。 我如何使用实体框架执行此操作? 我目前有:
var rolesForUser = _entities.Users
.Include("Roles")
.Where(u => u.Login.Equals(username))
.Select(u => u.Roles);
if(rolesForUser.Count() == 0) return false;
var authentications = _entities.WebPaths
.Where(p => p.Path == path)
.WhereIn(p => p.Roles, rolesForUser);
return (authentications.Count() > 0);
你可以用PredicateBuilder做到这一点。
脱离我的头顶:
var predicate = PredicateBuilder.False<WebPath>();
foreach (var role in from roles in rolesForUser
from r in roles.Role
select r)
{
predicate = predicate.Or (p => p.roles.Any(r => r.Id == role.Id));
}
var authentications = _entities.WebPaths.AsExpandable()
.Where(p => p.Path == path)
.Where(predicate);
return (authentications.Count() > 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.