简体   繁体   English

流利的NHibernate子类与外键的映射

[英]Fluent NHibernate Subclass mapping with foreign key

I have the this objects: 我有以下对象:

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

And the follow premises for mapping: 以及以下映射前提:
(a) The "relationshipToManagmentId" should be a foreign key. (a)“ relationshipToManagmentId”应为外键。
(b) The table "RelationshipToManagment" is a non-mapped table, (a old part of application) (b)表“ RelationshipToManagment”是一个非映射表,(应用程序的旧部分)
(c) The mapping strategie is TPT. (c)映射策略是TPT。 (at least for new objects :-) (至少对于新对象:-)

Mapping, until now: 映射,直到现在:

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!!

How can i write this? 我该怎么写?

Join() can only join on the primary key (property id) to the other table but you need to join on a foreign key column. Join()只能在主键(属性ID)上Join()到另一个表,但是您需要在外键列上Join() A normal Reference does what you want 普通参考可以满足您的需求

class Employee : Person
{
    Management Management;
}

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

Update: if you need readonly information from the RelationshipToManagment table you could use a formula property 更新:如果您需要从RelationshipToManagment表中获得只读信息,则可以使用公式属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM