So I've tried this mapping using and Override for the Business entity,
rather than its joined subclasses:
public class BusinessMap : IAutoMappingOverride<Business>
{
public void Override(AutoMap<Business> mapping)
{
mapping.JoinedSubClass<Manager>("BusinessId")
.HasManyToMany<Customer>(x => x.SecondaryCustomers)
.WithTableName("ManagersToCustomers")
.WithParentKeyColumn("ManagerFk")
.WithChildKeyColumn("CustomerFk");
mapping.JoinedSubClass<Manager>("BusinessId")
.HasMany<Customer>(x => x.ManagedCustomers)
.KeyColumnNames.Add("PrimaryManagerFk")
.Inverse();
mapping.JoinedSubClass<Customer>("BusinessId")
.HasManyToMany<Manager>(x => x.SecondaryManagers)
.WithTableName("ManagersToCustomers")
.WithParentKeyColumn("CustomerFk")
.WithChildKeyColumn("ManagerFk");
mapping.JoinedSubClass<Customer>("BusinessId")
.References<Manager>(x => x.PrimaryManager);
}
}
It seems odd to have to do it this way, but if you explicitly state
the inverse and the link table name, the mapping is done correctly:
<joined-subclass name="Project.Manager, Project, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="Managers">
<key column="BusinessId" />
...
<bag name="SecondaryCustomers" table="ManagersToCustomers">
<key column="ManagerFk" />
<many-to-many column="CustomerFk" class="Project.Customer,
Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</joined-subclass>
<joined-subclass name="Project.Customer, Project, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" table="Customers">
<key column="BusinessId" />
...
<bag name="SecondaryManagers" table="ManagersToCustomers">
<key column="CustomerFk" />
<many-to-many column="ManagerFk" class="Project.Manager, Project,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</joined-subclass>
However, the Inverse HasMany mapping does not work, giving me a
completely incorrect result under the Manager subclass:
<bag name="ManagedCustomers" inverse="true">
<key column="ManagerFk" />
<one-to-many class="Project.Customer, Project, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" />
</bag>
I'd expect the key column to be "PrimaryManagerFk" (as it was passed
into the mapping), and the one-to-many class to be "Project.Manager",
since one Manager has many Customers (this bag).
Any ideas what's going on? Why is the mapper not using my passed in
KeyColumnName. What automatic detection should I be able to rely on?
- Chris