This has got to be a FAQ of some sort but for the sake of it I can't find the answer anywhere... Just spent ~2hr reading the forums and trying things and I'm lost. Must be tired. Can you help?
I have the following structure:
User (Id,...) <-> UserRole (UserId,RoleId) <-> Role (Id,...) <-> Permission (RoleId,OperationId) <-> Operation (Id,...)
Find all users having at least one of some roles:
IRelationPredicateBucket rpb = ...;
IList<int> rolesAny = ...;
rpb.Relations.Add(UserEntity.Relations.UserRoleEntityUsingUserId);
rpb.PredicateExpression.Add(UserRoleFields.RoleId == rolesAny.ToArray());
Find all users having all of some roles--requires aliases:
IRelationPredicateBucket rpb = ...;
IList<int> rolesAll = ...;
foreach (int i in rolesAll)
{
string a = string.Format("a{0}", i);
rpb.Relations.Add(UserEntity.Relations.UserRoleEntityUsingUserId, a);
rpb.PredicateExpression.Add(UserRoleFields.RoleId.SetObjectAlias(a) == i);
}
Find all users having at least one of some permissions:
IRelationPredicateBucket rpb = ...;
IList<int> operationsAny = ...;
rpb.Relations.Add(UserEntity.Relations.UserRoleEntityUsingUserId);
IEntityRelation rel = new EntityRelation(UserRoleFields.RoleId, PermissionFields.RoleId, RelationType.OneToMany);
rpb.Relations.Add(rel, JoinHint.Inner);
rpb.PredicateExpression.Add(PermissionFields.OperationId == operationsAny.ToArray());
Find all users having all of some permissions--requires aliases:
IRelationPredicateBucket rpb = ...;
IList<int> operationsAll = ...;
foreach (int i in operationsAll)
{
string a = string.Format("a{0}", i);
string b = string.Format("b{0}", i);
rpb.Relations.Add(UserEntity.Relations.UserRoleEntityUsingUserId, a);
// FIXME
IEntityRelation rel = new EntityRelation(UserRoleFields.RoleId, PermissionFields.RoleId, RelationType.OneToMany);
rpb.Relations.Add(rel, JoinHint.Inner);
// FIXME
rpb.PredicateExpression.Add(PermissionFields.OperationId.SetObjectAlias(b) == i);
}
Obviously the lines enclosed between FIXME need to be fixed with aliases, and I can not find how. Have tried various combinations of:
IEntityRelation rel = new EntityRelation(
UserRoleFields.RoleId.SetObjectAlias(a),
PermissionFields.RoleId.SetObjectAlias(b),
RelationType.OneToMany);
rpb.Relations.Add(rel, a, b, JoinHint.Inner);
Still gives me the "Relation at index 1 doesn't contain an entity already added to the FROM clause. Bad alias?" exception.
Any idea?