2013-01-24 37 views
1

沒有得到這些數據是我的課:在我的倉庫ASP.NET MVC - 從其他表

public class HotelRoomRatingView 
    { 
     public int HotelID { get; set; } 
     public roomKinds RoomKinds { get; set; } 
     public HotelReview HotelReview { get; set; } 

     public HotelRoomRatingView() 
     { 
     } 
     public HotelRoomRatingView(int hotelId, roomKinds rooms, HotelReview review) 
     { 
      this.HotelID = hotelId; 
      this.RoomKinds = rooms; 
      this.HotelReview = review; 
     } 
    } 

public class HotelReview 
{ 
    public int HotelReviewID { get; set; } 

    public double Rating { get; set; } 

    public List<Review> Reviews { get; set; } 

    public HotelReview() 
    { 
     this.Reviews = new List<Review>(); 
    } 
} 

public partial class roomKinds : object, System.ComponentModel.INotifyPropertyChanged 
{ 
    [Key] 
    public int roomKindsId { get; set; } 

    private ObservableCollection<objectKind> objectKindField; 

    public roomKinds() 
    { 

    } 
    public roomKinds(Class.roomKinds origin) 
    { 
     this.objectKindField = origin.objectKind; 
    } 

    [System.Xml.Serialization.XmlElementAttribute(Order = 0)] 
    public ObservableCollection<objectKind> objectKind 
    { 
     get 
     { 
      return this.objectKindField; 
     } 
     set 
     { 
      this.objectKindField = value; 
      this.RaisePropertyChanged("objectKind"); 
     } 
    } 

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string propertyName) 
    { 
     System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged; 
     if ((propertyChanged != null)) 
     { 
      propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
public partial class Hotel : object, System.ComponentModel.INotifyPropertyChanged 
    { 
     [Key] 
     public int HotelId { get; set; } 
     private int hotIdField; 
     // many others properties 
     [System.Xml.Serialization.XmlElementAttribute(Order = 105)] 
     public HotelReview hotelReview 
     { 
      get; 
      set; 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Order = 104)] 
     public roomKinds rooms 
     { 
      get; 
      set; 
     } 

     [System.Xml.Serialization.XmlElementAttribute(Order = 0)] 
     public int hotId 
     { 
      get 
      { 
       return this.hotIdField; 
      } 
      set 
      { 
       this.hotIdField = value; 
       this.RaisePropertyChanged("hotId"); 
      } 
     } 
    } 

Get方法是這樣的:

public Models.Hotel Get(int id) 
     { 
      return db.hotels.SingleOrDefault(h => h.hotId == id); 
     } 

HotelIdhotId都還好,我需要它這條路。所以我從LINQ獲取數據庫中的酒店,我得到酒店,但我沒有從其他表中獲取數據。我沒有得到HotelReviewRoomKinds。我在這些屬性中得到了空值,但在數據庫中它們與Hotel通過ID連接。 感謝您的幫助

回答

3

您將需要使用.Include明確加載這些:

return db.hotels.Include(hr => hr.HotelReview) 
       .Include(rk => rk.RoomKinds) 
       .SingleOrDefault(h => h.hotId == id); 

UPDATE

假設你的實體正確鏈接,你應該能夠只是繼續向下包括:

return db.hotels.Include(hr => hr.HotelReview) 
       .Include(r => r.HotelReview.Reviews) 
       .Include(rk => rk.RoomKinds) 
       .SingleOrDefault(h => h.hotId == id); 
+0

它的工作。感謝您的更新。 –

+0

不是一個問題 - 很高興你把它理順了。 – ethorn10