in FMLab/ODataConsoleApplication/Program.cs [203:254]
private static Reservation CreateReservation(Customer customer)
{
Random rnd = new Random ();
//Create a new Reservation object with the details for new customer.
Reservation reservation = new Reservation()
{
Customer = customer,
PickupDate = DateTime.Today,
ReturnDate = DateTime.Today.AddDays (5.0),
State =0, //Limitation of OData, no client-side Enum support
ReservationId = "OData_" + rnd.Next (500).ToString ()
};
//Query the vehicle Odata Entity to retrieve the first vehicle from the list
var vehicle = (from v in context.Vehicles
select v).First();
// Set up the relationship between (a) reservation and vehicle and (b) reservation and customer.
//Add a link to the customer from the reservation
reservation.Customer = customer;
reservation.CustomerId = customer.RecId;
customer.Reservations.Add(reservation);
context.AddToReservations(reservation);
context.AddLink(customer, "Reservations", reservation);
//add link to the vehicle from the reservation
reservation.Vehicle = vehicle;
reservation.VehicleId = vehicle.RecId;
vehicle.Reservations.Add(reservation);
context.SetLink(reservation, "Vehicle", vehicle);
// Now that we have created a new reservation and set the required relationship with customer and vehicle,
// we will save these changes. Internally, this will also
// perform a POST to the Reservation OData Entity.
OperationResponse response = context.SaveChanges().First();
Console.ForegroundColor = ConsoleColor.Cyan;
if (response.Error == null)
{
Console.WriteLine("New Reservation created");
//Display the URI of the newly created reservation
Console.WriteLine(response.Headers["Location"]);
}
Console.ForegroundColor = ConsoleColor.White;
return reservation;
}