title: LINQ to Entities Foreign key associations
body: <p>I am new to asp.net, C# and sql and could use some guidance.. I am using the :below: db with the linq-to-entities framework. I need to associate a particular 'Ride' with a particular 'Vehicle' but am unsure exactly how to proceed. I have set a navigation property between the two objects but now need to let the vehicle hold a reference to a list of the rides it takes. Do I need a separate column in vehicle to hold this list of rides? Could someone please show me the syntax to accomplish this?</p>
  
  <p>Here is the code I currently have, with comments at the two spots I need help:</p>
  
  <pre><code>private void setNewRide(Ride newRide, int carNum)
          {
              using (myEntities = new RamRideOpsEntities())
              {
                  var assignedCar = (from car in myEntities.Vehicles
                                  where (car.CarNum == carNum)
                                  select car).FirstOrDefault();
  
                  if (assignedCar != null &amp;&amp; newRide != null)
                  {                
  
                      Ride lastRide = assignedCar.Rides.LastOrDefault(); //HERE IS WHERE I NEED TO LOAD THE MOST RECENT 'RIDE' FOR THIS CAR, IS THIS CORRECT???
  
                      if (lastRide != null)
                      {
                          lastRide.Status = "Completed";
                          assignedCar.numRides = assignedCar.numRides + 1;
                          lastRide.TimeDroppedOff = DateTime.Now;
                          TimeSpan duration = (lastRide.TimeDroppedOff - lastRide.TimeDispatched).Value;
  
                          lastRide.ServiceTime = duration;
                          if (assignedCar.AvgRideTime == null)
                          {
                              assignedCar.AvgRideTime = duration;
                          }
                          else
                          {
                              assignedCar.AvgRideTime = TimeSpan.FromSeconds(( ((TimeSpan)assignedCar.AvgRideTime).Seconds + duration.Seconds) / assignedCar.numRides);
                          }
                      }
  
                      assignedCar.Status = "EnRoute";
                      assignedCar.CurrPassengers = newRide.NumPatrons;
                      assignedCar.StartAdd = newRide.PickupAddress;
                      assignedCar.EndAdd = newRide.DropoffAddress;
  
                      //HERE IS WHERE I NEED TO ADD THE 'newRide' OBJECT TO THE LIST OF RIDES IN THE 'assignedCar' ..SYNTAX???
  
                      newRide.TimeDispatched = DateTime.Now;
                      newRide.AssignedCar = carNum;
                      newRide.Status = "EnRoute";
  
                      myEntities.SaveChanges();
                      SelectCarUP.DataBind();
                      SelectCarUP.Update();                    
                  }
              }
          }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/nibJe.png" alt="EDMX"></p>