views:

3653

answers:

1

Here are the domain model classes:

public abstract class BaseClass
{
...
}

public class ChildClass : BaseClass
{
...
}

Note that the parent class is abstract and this is what gives me some difficulties when comes the time to map with fluent nhibernate. My discriminator is a byte (tinyint in the DB). Because it is not a string and I can't manage to set a discriminator value on the base class, this does not work (taken from the mapping class for BaseClass):

DiscriminateSubClassesOnColumn<byte>("Type")
    .SubClass<ChildClass>()
    .IsIdentifiedBy((byte)OperationType.Plan)
    .MapSubClassColumns(p => { ... })

The error message I get is:

Class Initialization method UnitTest1.MyClassInitialize threw exception. NHibernate.MappingException: NHibernate.MappingException: Could not format discriminator value to SQL string of entity BaseClass ---> System.FormatException: Input string was not in a correct format..

The following post seems to explain what happens. They give a solution with xml but not with fluent nhibernate: http://forum.hibernate.org/viewtopic.php?t=974225

Thanks for the help.

+5  A: 

I have found a workaround but this seems so like a patch... I added the following to the mapping file:

SetAttribute("discriminator-value", "-1");

It seems to instruct FNH not to use a string (I think it uses the class name) for the abstract base class. To make it work with the -1 value, I also changed my discriminator type from byte to sbyte.

Edit: I missed that: this is the second parameter to DiscriminateSubClassesOnColumn that takes the default value. So the correct answer to my question is:

DiscriminateSubClassesOnColumn<sbyte>("Type", (sbyte)-1)
Nicolas Cadilhac
Much thanks - I was having a similar issue with the discriminator not being a string.
Ted