in FMLab/Odata4ConsoleApplication/Program.cs [179:229]
private static FleetRental CreateReservation(FleetCustomer customer)
{
Random rnd = new Random();
DataServiceCollection<FleetRental> reservations = new DataServiceCollection<FleetRental>(context);
FleetRental reservation = new FleetRental();
reservations.Add(reservation);
//Create a new Reservation object with the details for new customer.
reservation.CustomerFirstName = customer.FirstName;
reservation.CustomerLastName = customer.LastName;
reservation.CustomerDriverLicense = customer.DriverLicense;
reservation.State = 0; //Limitation of OData, no client-side Enum support
reservation.RentalId = "OData_" + rnd.Next(500).ToString();
reservation.Comments = "New customer";
reservation.VehicleId = "Adatum_Four_2";
reservation.VehicleVIN = "WAUXL58E15A104563";
reservation.EndDate = DateTime.Today.AddDays (5);
reservation.StartDate = DateTime.Today;
Microsoft.OData.Client.DataServiceResponse response = null;
try
{
response = context.SaveChanges(SaveChangesOptions.PostOnlySetProperties);
}
catch (DataServiceRequestException e)
{
Console.WriteLine("Error occured while saving. Error Details: " + e.InnerException.Message);
}
//the following code retrieves the location header which represents the newly created entity
Console.ForegroundColor = ConsoleColor.Cyan;
foreach (ChangeOperationResponse r in response)
{
if (r.Headers.ContainsKey("Location"))
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("New reservation created: ");
Console.WriteLine(r.Headers["Location"]);
}
}
Console.ForegroundColor = ConsoleColor.White;
return reservation;
}