实体框架5不会与Include()获取关系

时间:2013-05-20 00:19:16

标签: c# entity-framework-5

我很确定此类问题之前已经回答了很多次,但我无法得到任何建议。

我正在使用Entity Framework 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获取这种关系。

1 个答案:

答案 0 :(得分:1)

将我的评论复制到答案 - 将包括在实际查询中

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