'Is it possible to get NHibernate to auto generate a property from the ID?

I have the following property on a class persisted with NHibernate:

public virtual string Code
{
    get { return Id.ToString("SP000"); }
}

However I've come to do this, which of course doesn't work:

Session.Linq<MyType>().Where(x => x.Code.Contains(searchTerm));

So, is it possible to store Code in the database and have NHibernate manage the

.ToString("SP000")

to set Code, when ID is set?

Edit: I guess I need an interceptor, but I'm not sure how to implement what I need.



Solution 1:[1]

Map the code as a readonly property in your hbm, then you can search on it in whatever way you prefer !

As a side effect from mutating the id you should also consider adding a unique constraint to it, and setting the properties update=false property.

Kind regards

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nexus