2

我有一张员工表:

Employee
{
    Name
    EmployeeId -pk
    PositionId -fk
}

positionId 映射到位置表:

Position
{
    PositionId -pk
    ReportsToId
    PositionName
    PositionDescription
}

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

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

这将如何使用 NHibernate 的 Mapping.ByCode 来完成。

ReportsToId 字段不是关键字段。从我在网上阅读的内容来看,这似乎会影响映射......

4

1 回答 1

2

在这种情况下,映射将在5.1.10 之前。多对一的功能称为property-ref

<many-to-one
    ...
    property-ref="PropertyNameFromAssociatedClass"     (7)

(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,Mapping-by-Code - ManyToOne

ManyToOne(x => x.ManagerPosition , m =>
{ 
    ...             
    m.Column("PositionId") // column in the Employee table
    m.PropertyRef(propertyReferencedName) // the Property/column in the Position table
于 2014-04-07T06:12:27.070 回答