I am using entity framework, and require a way of making a collection available using linq. I’ll show what I mean.
I have 3 tables.
- Products
- ProductOptions
- Product_ProductOption_Mappings
I am using the Product_ProductOption_Mappings table to create a many-to-many relationship between Product and ProductOptions. Given a specific product, I would like to pull out a collection of it’s product options.
using((Model.Entities context = new Model.Entities())
{
var options = context.Products
.Where(p => p.ID == 1)
.First()
.Product_ProductOption_Mappings.ProductOptions.ToList();
}
Now this isn’t correct, but it is intended to return a list of ProductOptions that are ‘realted’ to the Products.
Any help would be kindly appreciated.
If your many to many table only contains the two Ids necessary for the management of the relationship EF should support hiding that linking table from your conceptual model. Basically you should just have a Product and ProductOption entity in your conceptual model with a Many-to-Many association between them. In the example below I have named the navigation property contained within Product “Options”.
You should then be able to do something like this:
to solve your problem.