我有一个现有的视图,其中包含太多数据。不幸的是我无法摆脱它所以我需要尝试使用我的NHibernate映射来解决它。我们的想法是让NH发出以下查询:
SELECT DISTINCT User_Id, Module_Id, Application_Id, RightsMask
FROM V_UserApplicationPermissions
WHERE User_Id = ?
我对此AccessControlEntry类型列表的当前映射如下所示:
HasMany<AccessControlEntry>(x => x.Rights)
.WithTableName("V_UserApplicationPermissions")
.KeyColumnNames.Add("User_Id")
.Component(c =>
{
c.Map(x => x.Module, "Module_Id");
c.Map(x => x.Application, "App_Id");
c.Map(x => x.Rights, "RightsMask").CustomTypeIs<ApplicationRightsType>();
})
.Not.LazyLoad();
有关如何让NHibernate在查询期间在其中放置DISTINCT关键字的任何想法?
更新:让我分享一下用户地图的其余部分,这可能有助于解释为什么它不是一个直接的标准:
WithTable("Users");
Id(x => x.Id, "UserId");
Map(x => x.Name, "UserName");
HasMany<long>(x => x.Clients)
.WithTableName("V_UserClients")
.KeyColumnNames.Add("UserId")
.AsElement("ClientId");
答案 0 :(得分:1)
来自NHUsers邮件列表的Olivier Coanet建议将其黑客入侵WithTableName:
HasMany<AccessControlEntry>(x => x.Rights)
.WithTableName("(SELECT DISTINCT User_Id, Module_Id, App_Id, RightsMask FROM V_UserApplicationPermissions)")
.KeyColumnNames.Add("User_Id")
.Component(c =>
{
c.Map(x => x.Module, "Module_Id");
c.Map(x => x.Application, "App_Id");
c.Map(x => x.Rights, "RightsMask").CustomTypeIs<ApplicationRightsType>();
})