表每个子类继承映射由NHibernate按代码映射 [英] Table Per Subclass Inheritance mapping by NHibernate Mapping-by-Code
本文介绍了表每个子类继承映射由NHibernate按代码映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在这个类的每个子类策略的新NHibernate Mapping-By-Code中编写映射:
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; }
}
推荐答案
这是一个可能的映射以略微缩写的形式
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);
}
}
您无需复制Id的声明派生类中的属性。它继承自父类Person。
You don't need to duplicate declaration of the Id property in the derived classes. It's inherited from the parent Person class.
这篇关于表每个子类继承映射由NHibernate按代码映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文