开发者

Retrieving distinct values in mapping

I have an existing view which has too much data in it. Unfortunately I cannot get rid of it so I need to try to work around it using my NHibernate mapping. The idea is to have NH issue the following query:

SELECT DISTINCT User_Id, Module_Id, Application_Id, RightsMask
FROM开发者_如何学Python V_UserApplicationPermissions
WHERE User_Id = ?

My current mapping for this list of AccessControlEntry types looks like this:

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();

Any thoughts on how to have NHibernate put a DISTINCT keyword in there during the query?

UPDATE: Let me share the rest of the User map that might help as to why it isn't a straight forward criteria:

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");


Olivier Coanet from the NHUsers mailing list suggested hacking it into the WithTableName which worked:

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>();  
    })  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜