Entity Framework AddTo object(s) with Foreign key
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
开发者_JS百科 {
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);
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.
精彩评论