I created a custom type for an Enum that I need persisted with 4-letter codes to the database, and in the overrides for the type for the set method, I am converting from the enum value to a string. While executing a find using criteria, this is failing with an InvalidCastException as NHibernate is passing the value of the enum in as a string. Here is the code from my type:
Code:
public override void Set(System.Data.IDbCommand cmd, object value, int index)
{
string enumString = ToString(value);
((System.Data.IDataParameter)cmd.Parameters[0]).Value = enumString;
}
public override string ToString(object val)
{
switch ((BusinessEntities.TicketStatus)val)
{
case BusinessEntities.TicketStatus.Open:
return "OPEN";
case BusinessEntities.TicketStatus.Closed:
return "CLSD";
case BusinessEntities.TicketStatus.Canceled:
return "CNCL";
default:
throw new ArgumentOutOfRangeException("value");
}
}
I think I tracked it down to a conversion that NHibernate does of any value to a string if ignore case is specified (I'm using an EqExpression). The relevant code (from SimpleExpression.cs) is below. Is this by design, or should it be considered a bug?
Code:
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
object icvalue = [b]_ignoreCase ? _value.ToString().ToLower() [/b]: _value;
return new TypedValue[]
{
criteriaQuery.GetTypedValue( criteria, _propertyName, icvalue )
};
}
Thanks,
-jason