如何在 nhibernate queryover 中执行此操作

发布于 2024-10-19 03:42:56 字数 2780 浏览 5 评论 0原文

我需要在 nhibernate QueryOver 中执行此操作:

select distinct sc.* from [Security].[Commands] sc
inner join [Security].[SystemSubjects] ss on ss.Id = sc.Id
left outer join [Security].[SystemSubjectRoles] ssr on ssr.SystemSubjectId = ss.Id
left outer join [Security].[Roles] sr on sr.Id = ssr.RoleId
left outer join [Security].[UserRoles] ur on ur.IdRole = sr.Id 
left outer join [Security].[User] su2 on su2.Id = ur.IdUser
left outer join [Security].SystemSubjectUsers ssu on ssu.SystemSubjectId = ss.Id
left outer join [Security].[User] su on su.Id = ssu.UserId
where (su2.Id = 1 or su.Id = 1)

到目前为止,我已经这样做了:

var queryOverRoles = QueryOver.Of<Command>(() => cmdAlias)
            .JoinAlias(() => cmdAlias.SystemSubjectRoles, () => ssRoleAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => ssRoleAlias.Role, () => roleAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => roleAlias.Users, () => userAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => cmdAlias.SystemSubjectUsers, () => ssUserAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => ssUserAlias.User, () => userAlias2, JoinType.LeftOuterJoin)
            .TransformUsing(Transformers.DistinctRootEntity)
            .Where(() => ssRoleAlias.SecurityPermission == SecurityPermission.Allow)
            .And(() => (userAlias.Id == userId || userAlias2.Id == userId))

但这会选择所有(选择 ),而我只需要 Command 对象(选择 sc.)。 如果我尝试向查询添加投影,如下所示:

queryOverRole.Select(x=>x);

它会抛出此异常:

System.Exception 被捕获 Message=无法确定成员

来自 x 来源=NHibernate
堆栈跟踪: 在NHibernate.Impl.ExpressionProcessor.FindMemberExpression(表达式 表达式)中 d:\CSharp\NH\nhibernate\src\NHibernate\Impl\ExpressionProcessor.cs:line 189 在 NHibernate.Criterion.QueryOver2.Select(Expression1[] 预测)在 d:\CSharp\NH\nhibernate\src\NHibernate\Criterion\QueryOver.cs:行 第363章 在 Novatec.Persistence.Repositories.Implementations.UserRepository.GetAllowedUserCommands(Int32 用户 ID) 中 C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Persistence\Repositories\Implementations\UserRepository.cs:line 140 在 Novatec.Administracija.Services.Security.SecurityManager.AuthorizeUserAction(Int32 用户 ID、字符串命令 ID) 中 C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Administracija.Services\Security\SecurityManager.cs:line 48 在 Novatec.Framework.Infrastruct.Services.ServiceBase.ExecuteCommand(Int32 用户 ID、字符串 appId、ICommand 命令)中 C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Framework.Infrastruct\Services\ServiceBase.cs:line 94 内部异常:

I need to do this in nhibernate QueryOver:

select distinct sc.* from [Security].[Commands] sc
inner join [Security].[SystemSubjects] ss on ss.Id = sc.Id
left outer join [Security].[SystemSubjectRoles] ssr on ssr.SystemSubjectId = ss.Id
left outer join [Security].[Roles] sr on sr.Id = ssr.RoleId
left outer join [Security].[UserRoles] ur on ur.IdRole = sr.Id 
left outer join [Security].[User] su2 on su2.Id = ur.IdUser
left outer join [Security].SystemSubjectUsers ssu on ssu.SystemSubjectId = ss.Id
left outer join [Security].[User] su on su.Id = ssu.UserId
where (su2.Id = 1 or su.Id = 1)

So far I have done this:

var queryOverRoles = QueryOver.Of<Command>(() => cmdAlias)
            .JoinAlias(() => cmdAlias.SystemSubjectRoles, () => ssRoleAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => ssRoleAlias.Role, () => roleAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => roleAlias.Users, () => userAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => cmdAlias.SystemSubjectUsers, () => ssUserAlias, JoinType.LeftOuterJoin)
            .JoinAlias(() => ssUserAlias.User, () => userAlias2, JoinType.LeftOuterJoin)
            .TransformUsing(Transformers.DistinctRootEntity)
            .Where(() => ssRoleAlias.SecurityPermission == SecurityPermission.Allow)
            .And(() => (userAlias.Id == userId || userAlias2.Id == userId))

but this selects all (Select ) and I want only the Command object (select sc.).
If I try to add a projection to the query like this:

queryOverRole.Select(x=>x);

it throws this exception:

System.Exception was caught   Message=Could not determine member

from x Source=NHibernate
StackTrace:
at NHibernate.Impl.ExpressionProcessor.FindMemberExpression(Expression
expression) in
d:\CSharp\NH\nhibernate\src\NHibernate\Impl\ExpressionProcessor.cs:line
189
at NHibernate.Criterion.QueryOver2.Select(Expression1[]
projections) in
d:\CSharp\NH\nhibernate\src\NHibernate\Criterion\QueryOver.cs:line
363
at Novatec.Persistence.Repositories.Implementations.UserRepository.GetAllowedUserCommands(Int32
userId) in
C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Persistence\Repositories\Implementations\UserRepository.cs:line
140
at Novatec.Administracija.Services.Security.SecurityManager.AuthorizeUserAction(Int32
userId, String commandId) in
C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Administracija.Services\Security\SecurityManager.cs:line
48
at Novatec.Framework.Infrastructure.Services.ServiceBase.ExecuteCommand(Int32
userId, String appId, ICommand
command) in
C:\Projects\EGrad\Main\Source\Novatec.E-Grad\Source\Novatec.Framework.Infrastructure\Services\ServiceBase.cs:line
94 InnerException:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2024-10-26 03:42:56

试试这个......您需要指定要选择的别名。
queryOverRole.Select(结果 => cmdAlias);

Try this.... you need to specify which alias to select.
queryOverRole.Select(result => cmdAlias);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文