I have a scenario that requires a mapping that needs to be done with joins to 2 tables via a middle table (see example) currently it is achieved via a formula which translates to a subquery, but i would like to use joins to do this more efficiently. please help
Map(x => x.PropertyX).Formula(@"
(SELECT C.ColumnX
FROM TableA A
JOIN TableB B
ON A.Id = B.Id
JOIN TableC C
ON C.Id = B.Id
WHERE A.ColumnY = 898)");
Solution you've used could be convertied into standard ORM use case. The native or object/entity oriented solution here would be to introduce
many-to-one
andone-to-many
mappings. That would bring lot of benefits (lazy loading == only if needed, querying...)Small note, I am expecting that the FORMULA snippet in the question is just an example, because A.Id = B.Id = C.Id would mean B is not needed...
So we should introduce entities:
and their mapping:
Now we can get the same result with standard query with few JOINs.
In case, that the
we can create a virtual entity - mapped to a view:
Where
viewName
would represent a view on a DB side. In case, we cannot introduce view, we can define it as an inlined SELECT