This is a simplified example of the actual schema, so please bear with me. I have a table foo with an id and a value column. There’s also a bar table with a FK back to the foo table, and this table has an extended_value column. I want to have Foo’s class map left join Bar and try to use it’s extended_value column if present, otherwise fall back to Foo’s value.
Here’s the SQL equivalent of what I need:
select coalesce(b.extended_value, f.value) as value
from foo f
left join bar b on (b.id = f.id)
I’m using Fluent on top of NHibernate. This is what I’ve been attempting to use:
Join("bar", m =>
{
m.Optional();
m.KeyColumn("id");
m.Map(foo => foo.Value).Formula("COALESCE(extended_value, value)");
});
But this is failing because the generated SQL is expecting both extended_value and value to be on the same table.
Any ideas?
My advice is to not do this in the mapping.
Map
foo.valueandbar.extended_valueindependently, and then usecoalescein your queries and/or create an unmapped property that uses the??operator.