2013-07-27 67 views
2

我試圖加載多層次的具有明確加載但出現錯誤:實體框架顯式加載多層次

「屬性路徑‘Channel.Posts’不能用於導航屬性屬性路徑只能用於訪問原始或複雜的屬性。「

這是我的代碼:

var listSubs; 

using (var db = new GamePortalContext()) 
{ 
    listSubs = db.Subscribers.Include("Channel").Where(o => o.User.Username == username.ToLower() && o.Channel.IsActive && o.Channel.IsPublic && o.Channel.Posts.Count(p => p.PublishTime <= DateTime.Now && p.IsActive && p.IsHot) > 0); 
    if (listSubs.Any()) 
    { 
     listSubs = listSubs.OrderByDescending(o => o.Channel.ChannelTrack.LastPublishTime); 
     listSubs = (num == int.MinValue) ? listSubs : listSubs.Take(num); 
     foreach (var item in listSubs) 
     { 
      db.Entry(item).Collection(o => o.Channel.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load(); 
     } 

     return listSubs.ToList(); 
    } 
    else 
    { 
     return null; 
    } 
} 

這裏是我的崗位和渠道實體

公共部分類帖子

{

public Post() 

    { 

     this.ReadPostLaters = new HashSet<ReadPostLater>(); 

    } 
    public string PostId { get; set; } 
    public string Name { get; set; } 
    public string Alias { get; set; } 
    public string Description { get; set; } 
    public bool IsActive { get; set; } 
    public bool IsHot { get; set; } 
    public System.DateTime CreatedAt { get; set; } 
    public System.DateTime PublishTime { get; set; } 
    public int Views { get; set; } 
    public bool IsSticked { get; set; } 
    public int UpdatedTime { get; set; } 
    public bool IsSaved { get; set; } 
    public Nullable<int> ChannelId { get; set; } 
    public long UserId { get; set; } 
    public int PostType { get; set; } 
    public string UrlAvatar { get; set; } 
    public virtual Article Article { get; set; } 
    public virtual Channel Channel { get; set; } 
    public virtual Event Event { get; set; } 
    public virtual User User { get; set; } 
    public virtual ICollection<ReadPostLater> ReadPostLaters { get; set; } 
    public virtual Video Video { get; set; } 
} 

公衆部分類頻道

{

public Channel() 
    { 
     this.Ads = new HashSet<Ad>(); 
     this.ChannelAdmins = new HashSet<ChannelAdmin>(); 
     this.ChannelPlayers = new HashSet<ChannelPlayer>(); 
     this.Notifications = new HashSet<Notification>(); 
     this.Posts = new HashSet<Post>(); 
     this.Subscribers = new HashSet<Subscriber>(); 
    } 
    public int ChannelId { get; set; } 
    public string Name { get; set; } 
    public string Username { get; set; } 
    public int Voters { get; set; } 
    public int Subs { get; set; } 
    public float SiteScore { get; set; } 
    public float UserScore { get; set; } 
    public string HomeUrl { get; set; } 
    public string FanpageUrl { get; set; } 
    public string Publisher { get; set; } 
    public int Players { get; set; } 
    public Nullable<System.DateTime> PublishDate { get; set; } 
    public string Status { get; set; } 
    public bool IsActive { get; set; } 
    public bool IsHot { get; set; } 
    public bool IsPublic { get; set; } 
    public bool IsNew { get; set; } 
    public bool IsChanneling { get; set; } 
    public int CategoryId { get; set; } 
    public string UrlAvatar { get; set; } 
    public string UrlCover { get; set; } 
    public virtual ICollection<Ad> Ads { get; set; } 
    public virtual CategoryChannel CategoryChannel { get; set; } 
    public virtual ICollection<ChannelAdmin> ChannelAdmins { get; set; } 
    public virtual ICollection<ChannelPlayer> ChannelPlayers { get; set; } 
    public virtual ChannelTrack ChannelTrack { get; set; } 
    public virtual ICollection<Notification> Notifications { get; set; } 
    public virtual ICollection<Post> Posts { get; set; } 
    public virtual ICollection<Subscriber> Subscribers { get; set; } 
} 
+0

安置自己的'Channel'類請,或任何類型的'Channel'是。 – Steve

+0

一個頻道包括帖子,頻道是一個實體從我的數據庫生成,並與帖子實體相關:一對多。 –

+0

你還應該發佈你的'Post'實體。 – Steve

回答

0

嘗試改變這一行:

listSubs = db.Subscribers.Include("Channel").Where(o => o.User.Username == username.ToLower() && o.Channel.IsActive && o.Channel.IsPublic && o.Channel.Posts.Count(p => p.PublishTime <= DateTime.Now && p.IsActive && p.IsHot) > 0); 

要還呼籲.Include("Channel.Posts")

listSubs = db.Subscribers.Include("Channel").Include("Channel.Posts") .. etc; 
+1

我想將過濾器應用到posts.EagerLoading(您的演示)不能應用過濾器。我能做什麼? –

3

變化

db.Entry(item).Collection(o => o.Channel.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load(); 

db.Entry(item.Channel).Collection(o => o.Posts).Query().Where(i => i.IsHot && i.IsActive && i.PublishTime <= DateTime.Now).Take(numpost).Load();