如何使用QueryOver API编写单向一对多查询?

时间:2016-05-15 01:18:56

标签: nhibernate queryover

考虑以下架构

Contact (note how there is no List<AuthorizationRoles>)
-Guid ContactId
-string ContactName

AuthorizationRole
-Guid AuthorizationRoleId
-string RoleName

ContactAuthorizationRole
-Guid Id
-Guid ContactId (foreign key)
-Guid AuthorizationRoleId (foreign key)

我想做NHibernate相当于这个SQL查询

select
    AR.RoleName
from
    ContactAuthorizationRole CAR join AuthorizationRole AR
        on
    CAR.AuthorizationRoleId = AR.AuthorizationRoleId
where
    CAR.ContactId = ?

我尝试了一些事情......

var temp = session.QueryOver<ContactAuthorizationRole>()
                .Where(x => x.Contact.Id == contactId)
                .JoinQueryOver<AuthorizationRole>(x => x.AuthorizationRole)
                .List();

然后我可以像这样访问

var roleName = temp[index].AuthorizationRole.RoleName;

我也试过

var temp2 = session.QueryOver<AuthorizationRole>()
                .JoinQueryOver(x => x.Id)
                .Select(x => x.RoleName)
                .List();

然后我可以这样访问

var roleName = temp2[index].RoleName;

我的第一次尝试对我来说似乎不对。我只想要一个基于ContactId的用户角色列表。我不想把这些简单的事情连在一起。第二次尝试抛出异常。我尝试过阅读documentation和Google-ing examples,但我很难理解如何使用QueryOver Api执行此操作。

如何使用QueryOver Api编写上述查询?

0 个答案:

没有答案
相关问题