NHibernate Inheritance Mapping
I know in NHibernate you can have inheritance mappings, and I know you can have table-per-class, table-per-subclass and table-per-concrete-class but they don't quite fit the scenario I have.
Basically what I want is to be able to have a开发者_JS百科 base class called product that looks like this:
public class BaseProduct
{
public virtual int ProductId {get;set;}
public virtual string ProductName {get;set;}
}
which maps directly to a product table.
Then I want to have a Product class which inherits from BaseProduct like this:
public class Product : BaseProduct
{
public virtual IList<Category> Categories {get;set;}
}
The thing is that the Product class should still map to the product table, the only difference being that this implementation has a list of Categories attached.
Without going into the technical reasons for why I need to do this, I would like to know if it's at all possible?
From your question and comment, I get that you want «Single Table Inheritance» [PoEAA, Fowler], but don't have the luxury of being able to add the needed discriminator to the table.
I've never run into this situation myself, but try to add a discriminator to your mapping which is a calculated value/derived field that uses sql to find out if there are foreign keys from Category (won't work for Products with empty Category collections though).
If your scenario is a read-only one, and you can add views to the DB, it's an option to map to a Product view with the discriminator calculated as stated above.
You are looking for a table perclass you need to set a discriminator-value NHDoc for inheritance
Do you have other classes that inherit from BaseProduct? If not, you can just map the Product class only.
精彩评论