NHibernate: InvalidCastException when using AliasToBeanResultTransformer

What happens to the following code (in NHibernate 2.1.2)?

        public IEnumerable<EmployeeSummary> List()
    {
        return Session.CreateCriteria<Employee>("e")
            .SetCacheable(true)
            .SetProjection(Projections.ProjectionList()
                            .Add(Projections.Property("e.Id"), "Id")
                            .Add(Projections.Property("e.CurrentOffice.Id"), "CurrentOfficeId")
            .SetResultTransformer(new AliasToBeanResultTransformer(typeof(EmployeeSummary)))
            .List<EmployeeSummary>();
    }

    public class EmployeeSummary
    {
        public Guid Id { get; private set; }
        public Guid CurrentOfficeId { get; private set; }
    }

I get the following error: NHibernate.Exceptions.GenericADOException: cannot search [SQL: SQL is unavailable] ----> System.InvalidCastException: Cannot cast object of type "EmployeeSummary" to type "System.Object []".

+3
source share
1 answer

Worked on this - the problem is in SetCacheable. You cannot use it with AliasToBeanResultTransformer.

This seems to be a bug / bug in the NHibernate function. Not sure if it is allowed in later versions.

+3
source

All Articles