2012-03-07 96 views
0

我有一个表,我所有的字典:“Id,TypeId,价值”,所以对于特定的TypeId我有一对:“Id +价值”这是我的具体字典。NHibernate通用字典表映射

我该如何映射它?现在我可以想象,我将有一个抽象类Dictionary和具体类:InvoiceTypeDictionary,PaymentTypeDictionary等(我可以将子类鉴别符设置为TypeId,就是这样)。但是有没有另外一种方法可以做到这一点,以避免必要的很多子类?

回答

0

我不确定什么是价值。

// InvoiceTypeMap : ClassMap<InvoiceType> 
public InvoiceTypeMap() 
{ 
    Table("dictionaryTable"); 
    Where("typeid=5"); 
    Map(it => it.SomeProperty, "value"); 
} 

// PaymentTypeMap : ClassMap<PaymentType> 
public PaymentTypeMap() 
{ 
    Table("dictionaryTable"); 
    Where("typeid=3"); 
    Map(it => it.SomeOtherProperty, "value"); 
} 


void SetInvoiceTypeToEntity(Invoice invoice, int invoicetypeid) 
{ 
    invoice.Invoicetype = session.Get<InvoiceType>(invoicetypeid); 
    // or when you only need the Reference without the actual object here 
    invoice.Invoicetype = session.Load<InvoiceType>(invoicetypeid); 
}