Multiple levels of inheritance with Fluent NHibernate using discriminators
Asked Answered
L

1

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.

Losse answered 16/11, 2011 at 16:21 Comment(0)
S
2
public BaseClassMap()
{
    Where("discriminatorColumn <> 'unknown'");
    // or
    Where("discriminatorColumn = 'known1' or discriminatorColumn = 'known2'");
}
Strauss answered 16/11, 2011 at 16:43 Comment(2)
I tried something like this, but it complains that discriminatorColumn isn't a known column.Losse
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 usefulStrauss

© 2022 - 2024 — McMap. All rights reserved.