Why your custom EnumStringType might not be working
I recently spent a couple hours yesterday to fix a problem I was having with my custom EnumStringType (see this post).
Basically, what was happening was when my repository was making a call to ICriteria, which in turn called the Set method of EnumStringType, it was always passing in a lowercase string instead of an enum instance.
For example, when I used a debugger to see what the object value argument was in this method:
/// <summary>
///
/// </summary>
/// <param name="cmd"></param>
/// <param name="value"></param>
/// <param name="index"></param>
public override void Set(IDbCommand cmd, object value, int index)
{
var par = (IDataParameter) cmd.Parameters[index];
if (value == null)
{
par.Value = DBNull.Value;
}
else
{
par.Value = Enum.Format(ReturnedClass, value, "G");
}
}
... it would show up as an "foo" string instead of a FooEnum.Foo enum object.
What was the problem? It turns out in my repository I was setting on of my criteria like this:
var criteria = Session.CreateCriteria
.Add( Expression.Eq("EnumField", FooEnum.Foo).IgnoreCase() );
Looks innocuous, does it not? Take a closer look: notice the IgnoreCase() call? That's what was causing it. It was there because previously I had not been using an enum and I had forgotten to remove it. That little sucker cost me 3 hours of my life.
Hopefully I saved you that time :)
1 comment