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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With