I have a table called products, and a mapping table called related_products which maintains a parent-child relationship between products, e.g. product SKU_ID 1 has related products 2 & 3.
products
+-----------------+
|SKU_ID | name |
+-----------------+
| 1 | Blah
| 2 | Blah2 |
| 3 | Blah3 |
+-----------------+
related_products
+---------+------------+
|SKU_ID_1 | SKU_ID_2 |
+---------+------------+
| 1 | 2 |
| 1 | 3 |
| 3 | 2 |
+---------+------------+
ORM Associations:
OneToMany relationship between products.SKU_ID and related_products.SKU_ID_1
OneToOne relationship between related_products.SKU_ID_2 and products.SKU_ID
This works fine in my application, but when I look at the returned objects in can see that because of the circular nature of the relationships, it seems like I’m returning too much data. Example: If I get all related products belonging to product SKU_ID=1, I get products 2 & 3 as expected. From those product objects I can also get their related products, then the related products of their related products and so on and so on.
Is this a problem? and if so how can I restrict the ‘depth’ of the returned associations? or have I done the mapping incorrectly in the first place?
I think the association like this is fine.
Since Linq2sql is lazy loading by default, there is no need to restrict the dept. You will only retrieve the related products as soon as you are using them in your code. (In case you are worried about loading too much, just profile the sql and you will see when/to what level they are loaded).
You might run into problems in case you are eager loading using LoadWith LoadOptions on the DC. I would not know how to deal with that.
Thanks to Jim Wooley I know that LoadOptions are not allowed with circular references….