Sunday, July 15, 2012

getting a row count in fluent NHibernate and fighting through a few errors

var count = session.CreateCriteria(typeof (RegisteredDriver))
      .SetProjection(Projections.RowCount());
int conversion = Convert.ToInt16(count);

 
 

I tried the approach above with fluent NHibernate to try to get a row count and got this error message:

Unable to cast object of type 'NHibernate.Impl.CriteriaImpl' to type 'System.IConvertible'.

 
 

I refactored to this:

var count = session.CreateCriteria(typeof (RegisteredDriver))
      .SetProjection(Projections.RowCount()).FutureValue<Int32>();
int conversion = count.Value;

 
 

I started to get this error:

Interceptor.OnPrepareStatement(SqlString) returned null or empty SqlString.

 
 

I read a thread online which suggested that my map was messed up. I looked into it the issue and realized I just didn't have a map. Duh. I made one.

using FluentNHibernate.Mapping;
using MvcAjax.Core;
namespace MvcAjax.Infrastructure
{
   public class RegisteredDriverMap : ClassMap<RegisteredDriver>
   {
      public RegisteredDriverMap()
      {
         Id(x => x.RegisteredDriverId);
         Map(x => x.LicenseNumber);
         Map(x => x.LicenseExpiration);
         Map(x => x.IsRequiredToWearGlasses);
      }
   }
}

 
 

I kept getting the following as an error even after adding the map:

No persister for: MvcAjax.Core.RegisteredDriver

 
 

I found I was looking into the wrong place in my repository for the map.

private static ISessionFactory CreateSessionFactory()
{
   return Fluently.Configure().Database(
   MsSqlConfiguration.MsSql2005
         .ConnectionString(c => c
            .FromAppSetting("FluentNHibernateConnection"))
            )
            .Mappings(m =>
            m.FluentMappings.AddFromAssemblyOf<RegisteredDriver>())
            .BuildSessionFactory();
}

 
 

I changed the reference to RegisteredDriver above to be to RegisteredDriverRepository which sat in the same namespace as RegisteredDriverMap. This fixed things.

No comments:

Post a Comment