I have a schema with an N:1
parent-child relationship that is stored in another table and is selected by a formula. Is it possible to map this entity to the parent using a formula?
public class ParentEntity {
public virtual int ParentId { get; set; }
public virtual ChildEntity Child{ get; set; }
}
public class ParentMapping : ClassMap<ParentEntity> {
public ParentMapping() {
Table("ParentTable");
Id(x => x.ParentId).Column("ParentId").GeneratedBy.Assigned().Not.Nullable();
References<ChildEntity>(x => x.Child).Formula(
@"(
SELECT TOP 1 ChildTable.ChildId
FROM ChildTable
WHERE ChildTable.ParentId = ParentId
)"
);
}
}
The SQL that this mapping generates looks like this:
SELECT
this_.ParentId,
this_.ChildEntity_id
FROM ParentTable this_
This is not what I'm looking for.
How can I reference this child entity and use, instead of ChildId
in the parent table, a formula that selects ChildId
from a formula?
I won't anyhow discuss the correctness of this approach, just try to answer. What you are trying to do: should work. I've checked the correctness of the formula in a test scenario. So, yes formula could be used exactly this way.
But because it is not working, I would a bit guess. Let's start with SQL generated in my test case, which is working.
Possible issues
I see two possible issues
1. Different Child ID column names
First of all in your snippet:
is column name of child primary key:
ChildId
while in SQL snippet is theChildEntity_id
:2. SQL Snippet does not match
Secondly, you mentioned that the (SQL Statement just above) is what was generated. But it is more like a statement of this mapping:
So couldn't be there some older/other mapping, which is in fact used?
SUMMARY I wanted to say, that this way of mapping is working. So you are on the correct track, but the devil is hidden in details ;)