Table Per Subclass Inheritance mapping by NHiberna

2019-04-01 15:24发布

How to write mappings in new NHibernate Mapping-By-Code in Table Per Subclass strategy for this classes:

public class Person
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

public class JuridicalPerson : Person
{
    public virtual int Id { get; set; }
    public virtual string LegalName { get; set; }
}

public class PrivatePerson : Person
{
    public virtual int Id { get; set; }
    public virtual bool Sex { get; set; }
}

1条回答
神经病院院长
2楼-- · 2019-04-01 16:02

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.

查看更多
登录 后发表回答