How can I construct a query against an custom IUserType field in NHibernate?
More specifically: I'm working on a brownfield application. I have a field in the database called "State" which contains a char representing what state a given object is in.
In my code I want this to be represented as an enum so I've created an enum with a value for each state and created an IUserType that converts from the db's char value to my enum and back for selects & updates.
I want to construct a query that looks something like this:
session.CreateCriteria<MyType>().Add(Expression.Eq("State", StateEnum.Complete))
However, that query throws an exception:
could not resolve property: State of: MyNamespace.MyType
presumably because NHibernate doesn't know how to do a select against the DB's char field given a StateEnum type.
Your class and mapping should be something like the following:
NHibernate has 3 built in mappers for enum:
(char) (int) enumvalue
.