tags:

views:

34

answers:

2

Hi

I have lots of tables and some of them have many relationships with other tables. I noticed the tables that have one relationship I am able to do what it is shown in NerdDinner Chapter 1.

Dinner dinner = db.Dinners.Single(d => d.DinnerID == 1);
RSVP myRSVP = new RSVP();
myRSVP.AttendeeName = "ScottGu";

dinner.RSVPs.Add(myRSVP);

So when I try something similar .add never shows up for me unless it has like one relationship.

So how do I make it work for multiple ones?

I am still not clear what this gets me anyways. Like does it save in typing? or what?

A: 

You need one add for each table in the relationship.

The Add method stages an object into the Linq to SQL data context, so that it can be added to its corresponding database table. The SubmitChanges method saves any pending changes to the database. So you can do all of your adds, and then commit them at once.

Robert Harvey
Can you give an example please. I am not following I been doing sql to linq for like 5 mins now lol. So I don't know how to make these add methods and still not 100% clear on them.
chobo2
+2  A: 
marc_s
single relation ship entity? Not sure what that is but I think I have some 0.1 relationships. How do I tell though. the diagram in mssql 2005 just show primary keys or 1 to many.
chobo2
I think I am getting it more now. Now with RSVPs you don't have to add the DinnerId. I am not 100% sure about the last couple lines especially with scottHunter.Dinner = upcomingDinner;. Why would you put all those people who are being RSVP into a RSVP person? Or would this make a Dinner object and add "Scott Hunter" as the hosted by and fill in some other fields.
chobo2
ASsigning the "Dinner" to "ScottHunter" will assign this dinner as the dinner ScottHunter plans to attend. It will establish an association between ScottHunter and the "upcomingDinner". From the "upcomingDinner", you'll now see a new "RSVP" object in its "RSVPs" EntitySet, called ScottHunter - he's been added to the list of attendees. From the ScottHunter RSVP object, you'll see the "UpcomingDinner" as the associated dinner he plans to attend. It's always a two-way association!
marc_s