2013-05-20 81 views
1

我非常確定像以前這樣的問題已經回答了很多次,但我無法得到任何工作建議。實體框架5無法使用包含獲取關係()

我正在用實體框架5構建一個MVC 4應用程序,其中實體是從現有表中生成的。我有一個看起來像這樣的實體類:

namespace RebuildingModel 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class StandardCodeTable 
    { 
     public StandardCodeTable() 
     { 
      this.StandardCodeTableTexts = new HashSet<StandardCodeTableText>(); 
     } 

     public int TableCode { get; set; } 
     public string RefTableName { get; set; } 

     public virtual ICollection<StandardCodeTableText> StandardCodeTableTexts { get; set; } 
    } 
} 

namespace RebuildingModel 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class StandardCodeTableText 
    { 
     public int TableCode { get; set; } 
     public string LanguageCode { get; set; } 
     public string TextVal { get; set; } 

     public virtual StandardCodeTable StandardCodeTable { get; set; } 
    } 
} 

namespace RebuildingSite.Models 
{ 
    public class CodeTableJoined 
    { 
     public int TableCode { get; set; } 
     public string ReferenceTableName { get; set; } 
     public string LanguageCode { get; set; } 
     public string TextValue { get; set; } 
    } 
} 

我有一個DAO,看起來像這樣:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace RebuildingModel.Dao 
{ 
    public class CodeTableDao 
    { 
     public CodeTableDao() { } 

     public ISet<StandardCodeTableText> GetCode(string refTableName) 
     { 
      HashSet<StandardCodeTableText> codes = new HashSet<StandardCodeTableText>(); 

      using (var db = new RebuildingTogetherEntities()) 
      { 
       db.StandardCodeTableTexts.Include("StandardCodeTables"); 

       var query = from c in db.StandardCodeTableTexts 
          where c.StandardCodeTable.RefTableName == refTableName 
          orderby c.TableCode 
          select c; 

       foreach (var item in query) 
       { 
        codes.Add(item); 
       } 
      } 

      return codes; 
     } 
} 

我有一個控制器,它看起來像這樣:

namespace RebuildingSite.Controllers 
{ 
    public class CodeTableController : Controller 
    { 
     public ActionResult Index(string refTableName) 
     { 
      CodeTableDao dao = new CodeTableDao(); 

      ICollection<StandardCodeTableText> codes = dao.GetCode(refTableName); 

      HashSet<CodeTableJoined> joins = new HashSet<CodeTableJoined>(); 

      foreach (var code in codes) 
      { 
       CodeTableJoined join = new CodeTableJoined(); 

       join.TableCode = code.TableCode; 
       join.LanguageCode = code.LanguageCode; 
       join.TextValue = code.TextVal; 
       join.ReferenceTableName = code.StandardCodeTable.RefTableName; 

       joins.Add(join); 
      } 

      ISet<string> refTableNames = dao.GetReferenceTables(); 

      ViewBag.RefTableNames = refTableNames; 

      return View(joins); 
     } 
    } 
} 

當我運行附加到控制器的視圖,在此行使用ObjectDisposedException引發關係:

join.ReferenceTableName = code.StandardCodeTable.RefTableName; 

這一定很簡單。我究竟做錯了什麼?我曾嘗試在很多不同的地方從上下文中添加Include()調用,甚至多次。
我也試過在Linq查詢中添加顯式連接。我無法獲得EF來獲取這種關係。

+2

應包括在實際查詢? 'var query = from db.StandardCodeTableTexts.include(「StandardCodeTables」)中的c。 其中c.StandardCodeTable.RefTableName == refTableName orderby c.TableCode select c;' –

+0

當然!我知道這很簡單。這真是愚蠢 - 感謝你指出了這一點。 – Brad

回答

1

複製我的評論一個答案 - 將包括在實際查詢

var query = from c in 
db.StandardCodeTableTexts.include("StandardCodeTables"). where 
c.StandardCodeTable.RefTableName == refTableName orderby c.TableCode 
select c;