ASP.NET MVC - 不从其他表获取数据

时间:2013-01-24 14:48:04

标签: asp.net-mvc database linq entity-framework ef-code-first

这些是我的课程:

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。我在这些属性中得到null,但在数据库中它们是{并且HotelID相关联。 谢谢你的帮助

1 个答案:

答案 0 :(得分:3)

您需要使用.Include

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

<强>更新

假设您的实体已正确关联,您应该可以继续在.Include中链接下来:

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