流利的nHibernate生成错误的查询

时间:2016-11-29 14:46:01

标签: c# sql-server pagination fluent-nhibernate

我正在尝试使用分页进行查询。它在我请求第一页时有效,但如果我在那之后请求任何页面我收到错误。

public ProgramMemberResultDTO GetSentFromList(SegmentFilterDTO filter)
{
        var sql = @"SELECT a.Email as Email, a.Phone as Cellphone, mp.mpFirstname as Firstname, mp.mpLastname as Lastname, ISNULL(b.ID, 0) as IsSent FROM
                        ListMember a LEFT JOIN
                        (SELECT this_.ID, this_.Email as Email, this_.Phone as Cellphone
                            FROM ListMember this_ 
                            left join SMSOutgoing sms on sms.PhoneNumber = this_.Phone 
                            left join CampaignSMS cs on cs.SMSID = sms.ID
                            WHERE this_.ListID = :listId AND cs.CampaignListID = :campaignListId) b on a.ID = b.ID
                            INNER JOIN MemberPrograms mp on a.ProgramProfileID = mp.ID
                            WHERE a.ListID = :listId";

        var criteria = _session.CreateSQLQuery(sql);

        criteria.SetParameter("campaignListId", filter.CampaignListId);
        criteria.SetParameter("listId", filter.SelectedList);

        var records = criteria.SetFirstResult(filter.PageNr)
            .SetMaxResults(filter.ResultsPerPage)
            .SetResultTransformer(Transformers.AliasToBean(typeof(ProgramMemberDTO))).List<ProgramMemberDTO>();

        var result = new ProgramMemberResultDTO()
        {
            Count = GetNrOfSentFromList(filter.CampaignListId, filter.SelectedList),
            PageNr = filter.PageNr,
            Rows = records
        };

        return result;
}

似乎nHibernate正在生成无效的查询,数据库无法运行。这是它生成的查询:

  

SELECT TOP(@ p0)FROM(,ROW_NUMBER()OVER(ORDER BY CURRENT_TIMESTAMP)as __hibernate_sort_row选择a.Email作为电子邮件,a.Phone作为手机,mp.mpFirstname作为名字,mp.mpLastname作为姓氏,ISNULL( b.ID,0)作为IsSent FROM ListMember LEFT JOIN(选择this_.ID,this_.Email作为电子邮件,this_.Phone作为Cellphone FROM ListMember this_ left加入SMSOutgoing sms on sms.PhoneNumber = this_.Phone left join CampaignSMS cs on cs.SMSID = sms.ID WHERE this_.ListID = @ p1 AND cs.CampaignListID = @ p2)b on a.ID = b.ID INNER JOIN MemberPrograms mp on a.ProgramProfileID = mp.ID WHERE a.ListID = @ p1 )作为查询WHERE查询.__ hibernate_sort_row&gt; @ p4 ORDER BY查询.__ hibernate_sort_row&lt;

修改

这是错误消息:

  

“无法执行查询   ↵[SELECT TOP(@ p0)FROM(,ROW_NUMBER()OVER(ORDER BY CURRENT_TIMESTAMP)as __hibernate_sort_row选择a.Email作为电子邮件,a.Phone作为手机,mp.mpFirstname作为名字,mp.mpLastname作为姓氏,ISNULL(b .ID,0)as IsSent FROM   ↵列表会员左下角   ↵(选择this_.ID,this_.Email为电子邮件,this_.Phone为手机   ↵FROM ListMember this_   ↵在sms.PhoneNumber = this_.Phone上加入SMSOutgoing短信   ↵在cs.SMSID = sms.ID上加入CampaignSMS cs   ↵在a.ID = b.ID上的WHERE this_.ListID = @ p1 AND cs.CampaignListID = @ p2)b   ↵INNERJOIN会员计划mp在a.ProgramProfileID = mp.ID上   ↵WHERE a.ListID = @ p1)作为查询WHERE query .__ hibernate_sort_row&gt; @ p4 ORDER BY查询.__ hibernate_sort_row]   ↵名称:campaignListId - 值:172名称:listId - 值:110   ↵[SQL:SELECT TOP(@ p0)FROM(,ROW_NUMBER()OVER(ORDER BY CURRENT_TIMESTAMP)as __hibernate_sort_row选择a.Email作为电子邮件,a.Phone作为手机,mp.mpFirstname作为名字,mp.mpLastname作为姓氏,ISNULL (b.ID,0)为IsSent FROM   ↵列表会员左下角   ↵(选择this_.ID,this_.Email为电子邮件,this_.Phone为手机   ↵FROM ListMember this_   ↵在sms.PhoneNumber = this_.Phone上加入SMSOutgoing短信   ↵在cs.SMSID = sms.ID上加入CampaignSMS cs   ↵在a.ID = b.ID上的WHERE this_.ListID = @ p1 AND cs.CampaignListID = @ p2)b   ↵INNERJOIN会员计划mp在a.ProgramProfileID = mp.ID上   ↵WHERE a.ListID = @ p1)作为查询WHERE query .__ hibernate_sort_row&gt; @ p4 ORDER BY查询.__ hibernate_sort_row]“

有什么想法吗?

0 个答案:

没有答案