Wednesday, January 25, 2017

Nhibernate SQL scalar query + SetCacheable

Nhibernate throws a cast error when CreateSQLQuery() is used with SetCacheable() and when trying to return a scalar value. The fix for this problem is to use the AddScalar() method. Thanks to this google group message.

    var sql = "Select count(*) as totalCount from myTables";
    var count = session.CreateSQLQuery(sql)
                   .AddScalar("totalCount", NHibernateUtil.Int32)                      .SetCacheable(true) 
                   .FutureValue<int>();

1 comment:

  1. Just what I needed. So few nHibernate resources out there. Thanks.

    ReplyDelete