views:

445

answers:

2

I have an entity context that includes three tables (see diagram here). The first is a table that contain products, the second contains recipes. The joining table has fields for IDs in both the products and recipes table as well as a 'bit' field called 'featured'.

I've searched and found no example on how to insert only how to select against this type of scenario.Does anyone have any suggestions on how this can be done? Thanks in advance for any help.

Cory

+1  A: 

I didn't do C# for a while, so I am not sure my syntax is valid, anyway this should be the idea:

Products product = new Products { Blah, Blah, Blah };

bool flag = false;
for (int i = 0; i < 5; i++)
{
    Products_Receipes pr = new Products_Receipes 
                                  { Products = product, IsFeatued = flag };
    pr.Receipes.Add(new Receipes()); 
    pr.Receipes.Add(new Receipes());
    flag = !flag;
}
Context.SaveChanges();

And if the above doesn't work, then let me just tell you that you have to create the main item (either Products or Receipes), then when you create the Products_Receipes set it's Products/Receipes property to the above (or by Products.Pruducts_Receipes.Add(pr)), then add the other side of the relation the same way.

Shimmy
thanks! i'll give it a try.c
Cory G
well it worked. Thanks for the help
Cory G
Consider voting / marking the question as answered.
Shimmy
+1 for it working :)
James Lawruk
A: 

An observation is that your diagram is structured more like a DB schema than an entity diagram. Entities should be designed to meet the business needs independent of the data storage structure so you can use any DB to store the data.

I believe you can remove the "Link" entity and set up a one-to-many or many-to-many associations between Products and Recipes. Then setup your DB schema w/ the link table and do the Table Mapping accordingly.

ChrisP
The only issue with this is the joining table has additional columns in it. Here's another example, where the joining table has additional data:http://thedatafarm.com/LearnEntityFramework/files/media/image/WindowsLiveWriter/ManytoManyRelationshipsintheEntityDataMo_852F/m2m3_thumb.png
Cory G