public int AddNewGuest(AllGuests myAG) { using (var context = new Sunshine_HotelEntities1()) { //get the last booking ID from the new booking, really difficult needed to google this hard var bookingId = Convert.ToInt16( context.Bookings.OrderByDescending(b => b.BookingID).Select(c => c.BookingID).First()); //CREATE new entry var newGuest = new Guest { Name = myAG.Name, Address = myAG.Address, NumberOfGuests = myAG.NumberOfGuests, RoomBooked = myAG.RoomBooked, BookingIDFK = bookingId, //get the booking ID from the booking table, this BookingDate = DateTime.Today.Date }; // newGuest.CheckIn = DateTime.Now; context.Guests.Add(newGuest); context.SaveChanges(); //need to reload the guest just saved to get back the GuestID in case the checkin is immediatly done, instead of being selected from the dgv, cannot use .last as SQL doesn't recognise it var lastGuest = context.Guests.OrderByDescending(g => g.GuestID).Select(g => g.GuestID).FirstOrDefault(); return lastGuest; } }
public void AddGuest() { AllGuests myAG = new AllGuests(); myAG.Name = Name; myAG.Address = Address; myAG.NumberOfGuests = NumberOfGuests; myAG.RoomBooked = RoomBooked; //run the query GuestId = myModelCalls.AddNewGuest(myAG); //reload the dgv LoadAllGuestsToDgv(); }