0

林取一個「粘貼」從功能波紋管一個倉庫,但我不能當我取粘貼應該包括訪問對象。 (Paste.FilterGroup.Ads)。如何包括使用存儲庫中的EntityFramework對象?

我的倉庫功能:

public T Find<T>(Expression<Func<T, bool>> predicate) where T : class 
    { 
     return Context.Set<T>().FirstOrDefault<T>(predicate); 
    } 

在我的代碼,我試試這個。

​​3210

編輯: 粘貼類包含

public virtual FilterGroup FilterGroup { get; set; } 

FilterGroup類包含

public virtual IEnumerable<Ad> Ads { get; set; } 

回答

0

您必須更換

public virtual IEnumerable<Ad> Ads { get; set; } 

通過

public virtual ICollection<Ad> Ads { get; set; } 

或其他實施ICollection<T>收集類型。 IEnumerable<T>不和EF會忽略它的導航屬性。

只要你的背景是活着你的導航性能應然後通過延遲加載加載。

你也可以支持預先加載在你的資料庫:

using System.Data.Entity; // required for the lambda version of Include 

// ... 

public T Find<T>(Expression<Func<T, bool>> predicate, 
    params Expression<Func<T, object>>[] includes) where T : class 
{ 
    IQueryable<T> query = Context.Set<T>(); 

    if (includes != null) 
     foreach (var include in includes) 
      query = query.Include(include); 

    return query.FirstOrDefault<T>(predicate); 
} 

然後調用它像這樣:

oPaste = _repository.Find<Paste>(x => x.PasteID == oPaste.PasteID, 
    x => x.FilterGroup.Ads); 
+0

謝謝!這解決了問題! –

相關問題