SHeinema
SHeinema

Reputation: 634

LINQ to Entities Foreign key associations

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?

Here is the code I currently have, with comments at the two spots I need help:

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 && 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();                    
                }
            }
        }

EDMX

Upvotes: 1

Views: 3067

Answers (2)

Steve Wilkes
Steve Wilkes

Reputation: 7135

For:

// HERE IS WHERE I NEED TO LOAD THE MOST RECENT 'RIDE' FOR THIS CAR, IS 
// THIS CORRECT???
Ride lastRide = assignedCar.Rides.LastOrDefault();

Use:

Ride lastRide = assignedCar.Rides
    .OrderByDescending(r => r.TimeDispatched)
    .FirstOrDefault();

...and for:

// HERE IS WHERE I NEED TO ADD THE 'newRide' OBJECT TO THE LIST OF RIDES 
// IN THE 'assignedCar' ..SYNTAX???

...your Vehicle entity has a Rides collection property already according to your model diagram, so you should just be able to say:

assignedCar.Rides.Add(newRide);

Finally - and purely as a matter of personal taste - instead of:

var assignedCar = (from car in myEntities.Vehicles
                   where (car.CarNum == carNum)
                   select car).FirstOrDefault();

I'd use:

var assignedCar = myEntities.Vehicles
    .FirstOrDefault(car => car.CarNum == carNum);

Upvotes: 4

In my opinion, the following articles are good places to start:

  1. EF 4.1 Code First Walkthrough
  2. Code First Relationships Fluent API. See Blog and Post entities, where Blog has a one-to-many relationship with Posts.

Upvotes: 1

Related Questions