views:

52

answers:

2

I am trying to work out how to manage the following relationships

A Store has many Products
A Product is in many Stores A Store knows how many of each Product it has

In the database I have 3 tables Stores, Products and a StoreProducts that has StoreId, ProductId and Quantity.

How would I map this in nHibernate or fluent nHibernate?

A: 

Your going to have to map a 3rd entity, I don't think the built in Many To Many functionality supports that. Would be cool if someone proved me wrong though.

mxmissile
Yup - I have come to that conclusion myself with Store having a collection of StoreProducts where I carry the specific store/product specific data and this in turn references the Product
Shaun
A: 

[This answer is general to ORM and not specific to FNH]

In the (very good) book NHibernate In Action (see Section 6.3.2) the authors express the opinion that it's almost never worthwile using a many-many mapping because you'll almost always discover (maybe later) that you want to attach extra meta-data to the 'link' between the two entities and so you may as well model this an an entitiy in it's own right (as you say have done in your question).

As you've discovered already, you need to create the StoreProducts entity and a many-one and a one-many to complete the association.

...just though you'd like to confirmation that this is a 'recommended' approach :-)

rohancragg