2012-02-05 175 views
3

如何寫新的NHibernate映射-BY-代碼映射表每個子類戰略本類:表每個子類繼承映射的NHibernate的映射,由碼

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; } 
} 
+2

看看這裏:http://notherdev.blogspot.com/2012/01/mapping-by-code-inheritance.html所有與代碼映射相關的繼承選項。 – NOtherDev 2012-02-05 15:48:39

回答

6

這裏是一個可能的映射略縮寫形式

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繼承。