1

I am quite certain that questions like this have been answered a number of times before, but I can't get any of the suggestions to work.

I am building a MVC 4 application with Entity Framework 5, where the entities were generated from existing tables. I have entity classes that look like this:

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; }
    }
}

I have a DAO that looks like this:

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;
        }
}

I have a controller that looks like this:

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);
        }
    }
}

When I run the view attached to the controller, an ObjectDisposedException is thrown at this line, where the relationship is used:

join.ReferenceTableName = code.StandardCodeTable.RefTableName;

This has to be something simple. What am I doing wrong? I have tried adding that Include() call in from the context in many different places, even multiple times.
I've also tried adding an explicit join in the Linq query. I can't get EF to fetch that relationship.

2
  • 2
    SHould the include be in the actual query? ` var query = from c in db.StandardCodeTableTexts.include("StandardCodeTables"). where c.StandardCodeTable.RefTableName == refTableName orderby c.TableCode select c;` Commented May 20, 2013 at 0:56
  • Of course! I knew it was something simple. That was dumb of me - thanks for pointing that out. Commented May 20, 2013 at 2:44

1 Answer 1

1

Copying my comment to an answer - Put the include be in the actual query

 var query = from c in
 db.StandardCodeTableTexts.include("StandardCodeTables"). where
 c.StandardCodeTable.RefTableName == refTableName orderby c.TableCode
 select c;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.