2012-03-07 89 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); 
}