This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
Imagine you have a class like this:
You might be tempted to issue a query such as this:
Unfortunately, this will not work, because FullName is not a mapped property, and will result in an exception being thrown.
You have three options:
- Change the FullName property so that it is a formula;
- Use LINQ to Objects to do a client-side query instead;
- Perform a different query.
Regarding option 1, you can define this property using mapping by code as:
Whereas for option 2, you have to have all entities materialized and then apply the condition:Be aware that you are first bringing ALL records from the database and only filtering the in-memory data.
As for option 3, it’s pretty straightforward:
There may be far more complex cases, however, when you will not be able to do this so easily, for example, if you have complex logic in your composite property.