7

I'm in the enviable situation of having to integrate with a legacy database, luckily for readonly purposes, and have chosen to use NHibernate. Up until now everything has been great, but I have a new requirement which has me scratching my head.

Before today I had one column in the table that would act as a discriminator, but now it turns out that in some cases I need to have more than one discriminator column. Is this possible with NHibernate?

I've looked into using formulas, which works, but now I have the issue that I need to exclude 'unknown' subclasses (ones that don't yet have a mapping). For example I have this:

DiscriminateSubClassesOnColumn("")
    .Formula("case ... when ... then ... when .. then ... else 'unknown' end");

I'd like to be able to filter out everything that is 'unknown'...

Edit: I think that a possible solution would be to use AlwaysSelectWithValue(), what implications does enabling this have? I believe it's the same as force in the nhibernate mapping xml.

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
jonnii
  • 28,019
  • 8
  • 80
  • 108

1 Answers1

2
public BaseClassMap()
{
    Where("discriminatorColumn <> 'unknown'");
    // or
    Where("discriminatorColumn = 'known1' or discriminatorColumn = 'known2'");
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • I tried something like this, but it complains that discriminatorColumn isn't a known column. – jonnii Nov 16 '11 at 16:46
  • 1
    *discriminatorColumn* must be the column you are discriminating on in the formula. NH doesnt care at all which column you are specifying, but it must be in the database to be useful – Firo Nov 16 '11 at 17:28