2013-05-20 57 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;