Hello
I need some help post data to two tabels using entity framework. To explain this best as possible, I have two tabels (Booking and Passenger)
The Booking tabel has been created based on the current model:
public class Booking { public int Id { get; set; } public string BookingNumber { get; set; } public DateTime OutboundDate { get; set; } public DateTime ReturnDate { get; set; } public string Route { get; set; } public string ReturnRoute { get; set; } public string Passengers { get; set; } public string Pet { get; set; } public string VehicleType { get; set; } // Passengers public virtual ICollection<Passenger> PassengersList { get; set; } }
As you see, this is a booking and it can have a list of passenger (PassengersList) which is the second tabel and is based on this model:
public enum PassengerType { Adult, Children, Infant } public enum Title { Mr, Mrs } public class Passenger { public int Id { get; set; } public int BookingId { get; set; } public Title? Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public PassengerType? PassengerType { get; set; } }
Have created a bookingContext class which has the following methods:
// DbSet to bookings public DbSet<Booking> Bookings { get; set; } public DbSet<Passenger> Passengers { get; set; }
Have made some dummy data using another class called BookingsInitializer.
Can load all the bookings using the Bookings method in the BookingContext class which is working fine. Now I have an object which is fully updated based on user input, and I want to post the data on to the database.
I'm retrieving the current booking which should be changed by this line:
// Get booking from database var bookingFromDatabase = _db.Bookings.Find(booking.Id);
Now bookingFromDatabase is loaded and I can change it with the values I had from another object. How can i post these data back to the tabels?
Any suggestions? Also how can I check if the data has been loaded to the tabels succesfully? I guess with some sort of exception handling..
Hope someone can help me :)