Recently I came across what I thought to be a very odd behavior of NHibernate. The following is basically some mappings I had defined for my Model:
public class BaseMapping: ClassMap<BaseClass>
{
public BaseMapping()
{
Id(x => x.Id);</div>
DiscriminateSubClassesOnColumn("IsClassA", true).AlwaysSelectWithValue();
}
}
public class ClassAMapping: SubclassMap<ClassA>
{
public ClassAMapping()
{
DiscriminatorValue(true);
}
}
public class ClassBMapping: SubclassMap<ClassB>
{
public ClassBMapping()
{
DiscriminatorValue(false);
}
}
Using AlwaysSelectWithValue forces NHibernate to use the constraint on all the queries it generates for Class A or Class B.
To be honest, I’m not quite sure why this isn’t the default, since situations arise that can be very weird, especially when dealing with collections of ClassA or ClassB. I recently had a situation where I queried for a list of ClassA elements, but it was returning all base types, meaning it wasn’t excluding ClassBs in my query.

