2016-06-02 4 views
1

현재 계정의 모든 예약을 나열 할 수 있지만 문제가있는 페이지에서 작업하고 있습니다. 예약에 지정된 방에서 모든 속성에 액세스하고 싶습니다. 그러나이를 달성하는 방법을 잘 모르겠습니다.테이블의 ID에서 속성에 액세스하는 방법

예약 테이블 : enter image description here

룸 모델 :

public class Room 
{ 
    public int RoomID { get; set; } 

    public int RoomNumber { get; set; } 

    public string RoomType { get; set; } 

    public string Image { get; set; } 

    public int Price { get; set; } 

    public int Adults { get; set; } 

    public int Childs { get; set; } 

    public bool SmokingRoom { get; set; } 

    public bool IsOccupied { get; set; } 

    public Floor Floor { get; set; } 

    public virtual ICollection<Booking> Bookings { get; set; } 
} 

예약 모델 :

public class Booking 
{ 
    public int BookingID { get; set; } 

    public Account Account { get; set; } 

    public DateTime DateOfReservation { get; set; } 

    public DateTime Arrival { get; set; } 

    public DateTime Departure { get; set; } 

    public int Adults { get; set; } 

    public int Children { get; set; } 

    public Room Room { get; set; } 

    public bool CheckIn { get; set; } 

    public bool CheckOut { get; set; } 

    public string Status { get; set; } 

    } 

Databasehandler있어서

 public static List<Booking> GetAllBookingsByAccount(int id) 
     { 
     HotelDbContext context = new HotelDbContext(); 

     var bookings = context.Bookings.Where(b => b.Account.AccountID == id).ToList(); 

     return bookings; 
     } 

보기 :

감사합니다! 당신은 EF가 속성을 재정의 할 수 있도록 가상으로 속성을 설정해야 할 일을

var bookings = context.Bookings.Include("Room").Where(b => b.Account.AccountID == id).ToList(); 
+0

EF를 사용하고 Room 개체를 Booking 개체에 대해 가상으로 사용하려면 드릴 다운 할 수 있어야합니다. 그렇지 않다면 두 번째 쿼리를 설정해야합니다. 클래스 정의를 게시 할 수 있습니까? – nurdyguy

+0

@nurdyguy 질문에 – josephzigler

+1

을 추가했습니다. 디버거를 사용하여 단계를 밟으면 방으로 드릴 다운 할 수 있습니까? 또한, 당신은 Entity Framework를 사용하고 있습니까? (nm에서 태그를보십시오 ...) – nurdyguy

답변

2

지연로드를 들면 :

1

당신은 예약에 방을 포함해야합니다. 즉, Include 메서드를 호출 할 필요가 없습니다.

업데이트에 대한 데이터 모델 : lazyload하는 EF을 가능하게하고 당신이 어떤 추가 코드없이 그 개체의 모든 속성에 액세스 할 수 있습니다

public class Booking 
{ 
    public int BookingID { get; set; } 

    public virtual Account Account { get; set; } 

    public DateTime DateOfReservation { get; set; } 

    public DateTime Arrival { get; set; } 

    public DateTime Departure { get; set; } 

    public int Adults { get; set; } 

    public int Children { get; set; } 

    public virtual Room Room { get; set; } 

    public bool CheckIn { get; set; } 

    public bool CheckOut { get; set; } 

    public string Status { get; set; } 
} 

.

+1

감사합니다! 방을 디버깅하는 동안 null이며이 포함 된 모든 속성을 지금 액세스 할 수 있습니다. :) – josephzigler

관련 문제