Table Per Subclass Inheritance mapping by NHibernate Mapping-by-Code

折月煮酒 提交于 2019-12-03 17:27:42

Here is a possible mapping in a slighly abbreviated form

public class PersonMapping : ClassMapping<Person>
{
    public PersonMapping()
    {
        Table("person");
        Id(x => x.Id, m => m.Generator(Generators.Native));
        Property(x => x.Name);
    }
}

public class JuridicalPersonMapping : JoinedSubclassMapping<JuridicalPerson>
{
    public JuridicalPersonMapping()
    {
        Table("juridical_person");
        Key(m => m.Column("person_id"));
        Property(x => x.LegalName);
    }
}

public class PrivatePersonMapping : JoinedSubclassMapping<PrivatePerson>
{
    public PrivatePersonMapping()
    {
        Table("private_person");
        Key(m => m.Column("person_id"));
        Property(x => x.Sex);
    }
}

You don't need to duplicate declaration of the Id property in the derived classes. It's inherited from the parent Person class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!