NHibernate.QueryException with what appears to be a simple query expression
We're attempting to implement the specification pattern in our DAL but are running into some problems. It appears that NHibernate is unable to transform the expression into something usable, but I'm not 100% certain if that is the issue.
I have a customer object that contains a social security number object that in turn contains a property called LastSegment that simply returns the last four digits of the ssn. In creating a specification that searches against the last segment of a ssn, we came up with the following expression where value is the search criteria passed in by the user:
a => a.Customer.Ssn.LastSegment == value
Seems fairly simple. However, when this is run, an exception is throw with the following error:
NHibernate.QueryException : could not resolve property: Ssn.LastSegment of: [namespace].Customer
Am I inter开发者_运维技巧preting the error correctly in that NHibernate cannot translate my expression into something it can use? Is there any way to get around this?
As a further note, we also get this error when we call ToString()
on some objects.
EDIT: Digging into the SocialSecurityNumber object, I noticed it is a struct instead of a class. Could that cause an issue?
It looks like LastSegment is a property that you added in a partial class. You won't be able to use that in your nhibernate expression. You will need to change your where
expression to use the actual property that is mapped to your database (Whatever LastSegment
is referencing).
If Segment
is a string you might be able to do something like this:
a => a.Customer.Ssn.Segment.SubString(someNumber, 4) == value
精彩评论