如何使用NHibernate Mapping.ByCode在非主键字段上加入表?

我有一张员工表:

Employee { Name EmployeeId -pk PositionId -fk } 

positionId映射到位置表:

 Position { PositionId -pk ReportsToId PositionName PositionDescription } 

ReportsToId字段是该职位经理的职位ID。

我想选择一名员工,他们的职位和经理的详细信息。

如何使用NHibernate的Mapping.ByCode完成这项工作。

ReportsToId字段不是关键字段。 从我在网上看到的,这似乎影响了映射……

在这种情况下的映射将是5.1.10。 多function一个名为property-ref的function:

  

(7) property-ref :(可选)连接到此外键的关联类的属性的名称。 如果未指定,则使用关联类的主键。

因此,Position类应具有ID和属性ReportsToId

 public virtual int ID { get; set; } public virtual int ReportsToId { get; set; } 

Employee C#类将具有以下属性:

 public virtual Position ManagerPosition { get; set; } 

并且Employee的属性ManagerPosition的映射将是(参见: Adam Bar,按代码映射 - ManyToOne )

 ManyToOne(x => x.ManagerPosition , m => { ... m.Column("PositionId") // column in the Employee table m.PropertyRef(propertyReferencedName) // the Property/column in the Position table