views:

441

answers:

1

The map.

    public SocialCodeMap()
    {
        Id(x => x.SocialCodeId);
        Map(x => x.Name);
        Map(x => x.Code);
        Map(x => x.DisplayOrder);
    }

And the Class.

public class SocialCode
{
    public virtual Guid SocialCodeId { get; set; }
    public virtual string Name { get; set; }
    public virtual char Code { get; set; }
    public virtual int DisplayOrder { get; set; }
}

And the call.

    public SocialCode FetchByCode(char code)
    {
        return Session.CreateCriteria<SocialCode>().Add<SocialCode>(x => x.Code == code).UniqueResult<SocialCode>();
    }

I receive this error when running FetchByCode().

System.Exception: Cannot convert '65' to System.Char
at NHibernate.LambdaExtensions.ExpressionProcessor.ConvertType(Object value, Type type)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression)
at NHibernate.LambdaExtensions.ExpressionProcessor.ProcessExpression<T>(Expression`1 expression)
at NHibernate.LambdaExtensions.ICriteriaExtensions.Add<T>(ICriteria criteria, Expression`1 expression)
at DAL.NHibernate.xxx.SocialCodeRepository.FetchByCode(Char code) in SocialCodeRepository.cs: line 18
at UnitTests.DAL.xxx.yyy.SocialCodeRepositoryFixture.FetchByCode_ReturnsNullCodeDNE() in SocialCodeRepositoryFixture.cs: line 38

It seems NHibernate is convering my char to an int somewhere along the way. How can I force it to use the char(1) type that is on the column?

+1  A: 

I assume Fluent-NH can automatically detect char type correctly. But if it can't, you can always show a hint to it. Try this

Map(x => x.Code).CustomType<NHibernate.Type.CharType>();

Update:

Sorry that I haven't tested the code. Well I tried and found that the bug is in NH Lambda Extensions. In short,

// mapping
Map(x => x.Code);

// querying
criteria.Add(Restrictions.Eq("Code", 'A'));

I tested this should work.

Canton
Thanks for the idea. I get a new exception when I try this:NHibernate.MappingException: Could not instantiate IType CharType: System.MissingMethodException: No parameterless constructor defined for this object.This occurs on the line where I build my SessionFactory.SessionFactory = configuration.BuildSessionFactory();Thoughts?
ddc0660