当前位置 : 主页 > 手机开发 > 其它 >

表每个子类继承映射由NHibernate映射代码

来源:互联网 收集:自由互联 发布时间:2021-06-19
如何在这个类的每个子类策略的新NHibernate Mapping-By-Code中编写映射: public class Person{ public virtual int Id { get; set; } public virtual string Name { get; set; }}public class JuridicalPerson : Person{ public virtua
如何在这个类的每个子类策略的新NHibernate Mapping-By-Code中编写映射:

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; }
}
这是一个略微缩写形式的可能映射

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);
    }
}

您不需要在派生类中复制Id属性的声明.它继承自父类Person.

网友评论