1

我很确定这样的问题以前已经回答过很多次了,但我无法得到任何可行的建议。

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

4

1 回答 1

1

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

 var query = from c in
 db.StandardCodeTableTexts.include("StandardCodeTables"). where
 c.StandardCodeTable.RefTableName == refTableName orderby c.TableCode
 select c;
于 2013-05-20T03:38:53.067 回答