2

I have 2 table Reservation Guest

Reservation has a FK to Guest (GuestId)

When I add a new reservation I have to call AddTo on both objects for it to work. Which is fine, but I was thinking I should be able to set the guest object in the Reservation initializer and just call AddTo on the reservation object.

Is this the correct approach or am I missing something?

                var guest = new Guest
                {
                    GuestId = Guid.NewGuid(),
                    LastName = item.Guest.LastName,
                    FirstName = item.Guest.FirstName
                };

                var reservation = new Reservation
                {
                    ReservationId = Guid.NewGuid(),
                    GuestId = guest.GuestId,
                    //Guest = guest,
                    ReservedDtm = item.Date
                };

                _context.AddToGuests(guest);
                _context.AddToReservations(reservation);
1
  • By the way: programming entity framework second edition from Julia Lerman worth reading Commented Apr 22, 2011 at 19:46

1 Answer 1

2

Create the guest and use addObject(newguest) on the context. After that create the reservation and add the guest to it. That should work. I type from my smartphone so forgive me the missing formatation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.