简体   繁体   中英

How to load navigation data in Entity framework Code first?

I have the following situation:

http://www.mediafire.com/view/?brjyqlj4rvjako9

When you read the picture in your browser you can see I have a three tables. The middle one is junction table.

All works well except the field Room in middle table (Bookings table) that always has a NULL value: http://www.mediafire.com/view/?axz7ljskgcpl8cp

So, I saw that it needs to be loaded by hand before I make a reading operation on it. How is this done in Entity Framework Code First as my project is running on it?

The relationship is many to many between guest and Room, Booking is a junction table. Thank you

Is the property virtual?:-

public class Booking
{
  ...

  public int RoomId { get; set; }
  public virtual Room Room { get; set; } // Needs to be virtual

  ...
}

Navigation properties need to be virtual in order to enable lazy-loading.

If that's not the problem then I'll need you to post the source for your three entities (Room, Guest, Booking)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM