2013-04-01 23 views
0

我的頁面調用一個使用Generic Repository「Find」方法的服務層方法。在服務層的方法,我做到以下幾點:ObjectDisposedException使用Include時 - 爲什麼?

using (IUnitOfWork unitOfWork = new DBContext()) 
       { 
        GenericRepository<Operator> operatorRepos = new GenericRepository<Operator>(unitOfWork); 
        { 
         try 
         { 
          var oper = operatorRepos.Find(o => o.OperatorID == operatorID).Include(o => o.cmn_Address).Single(); 
          return oper; 
         } 
         catch (InvalidOperationException exc) 
         { 
          //handle exception 
         } 
        } 
       } 

我的資料庫查找方法:

public IQueryable<T> Find(Func<T, bool> predicate) 
     { 
      return _objectSet.Where<T>(predicate).AsQueryable(); 
     } 

在頁面上,我嘗試訪問運營商的cmn_address導航屬性和我出現以下錯誤:

ObjectContext實例已被處置,不能再用於需要連接的操作。

我意識到,這是由using語句引起的處置方面,但我認爲包括方法急於負荷cmn_Address對象。我不明白爲什麼這不像我預期的那樣工作。

回答

1

你在你的WHERE條件使用Func<>,而不是Expression<Func<>>。這使它成爲Linq對象。這種改變是永久的。調用AsQueryable不會使其再次成爲Linq-to-entities。

+0

+1因爲你是實體框架的教父,你搖滾! –

相關問題