使用外键的流畅NHibernate子类映射

时间:2012-05-24 07:47:23

标签: c# .net fluent-nhibernate fluent-nhibernate-mapping

我有这个对象:

class Person { Int32 id; String name; /*..*/ Adress adress; }
class Employee : Person { String e_g_Tax; /*..*/ Guid relationshipToManagmentId; }

以下地图用于制图:
(a)“relationshipToManagmentId”应为外键 (b)表“RelationshipToManagment”是一个非映射表,(应用程序的旧部分)
(c)制图战略是TPT。 (至少对于新对象: - )

映射,直到现在:

public class PersonMap : ClassMap<Person> {
  public PersonMap(){
    Id(x => x.id);
    Map (x => x.Nachname).Length(255).Not.Nullable();
    /*..*/
    References(x => x.Adresse).Class(typeof(Adresse)).Not.Nullable();
  }
}
public class EmployeeMap : SubclassMap<Employee>
    {
        public EmployeeMap()
        {
            Map(x => x.e_g_Tax, "enjoytax")
                .Not.Nullable();
            /*..*/
            Join("RelationshipToManagment", xJoin =>
            {
                //xJoin.Table("RelationshipToManagment");
                xJoin.Fetch.Join();
                xJoin.KeyColumn("ID");
                xJoin.Map(x => x.relationshipToManagmentId)
                    .Not.Nullable() ;
            }); // --> exception!!

我怎么写这个?

1 个答案:

答案 0 :(得分:0)

Join()只能将主键(属性id)连接到另一个表,但您需要加入外键列。普通参考可以做你想要的事情

class Employee : Person
{
    Management Management;
}

public EmployeeMap()
{
    References(x => x.Management).Column("relationshipToManagmentId");
}

更新:如果您需要来自RelationshipToManagment表的只读信息,您可以使用公式属性

public EmployeeMap()
{
    Map(x => x.RelationshipToManagment).Formula("(SELECT m.Title FROM RelationshipToManagment m WHERE m.Id = relationshipToManagmentId)");
}