c# - Entity Framework 5 Won't Fetch Relationships With Include() -
i quite questions have been answered number of times before, can't of suggestions work.
i building mvc 4 application entity framework 5, entities generated existing tables. have entity classes 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 dao looks 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 = c in db.standardcodetabletexts c.standardcodetable.reftablename == reftablename orderby c.tablecode select c; foreach (var item in query) { codes.add(item); } } return codes; } } i have controller looks 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 run view attached controller, objectdisposedexception thrown @ line, relationship used:
join.referencetablename = code.standardcodetable.reftablename; this has simple. doing wrong? have tried adding include() call in context in many different places, multiple times.
i've tried adding explicit join in linq query. can't ef fetch relationship.
copying comment answer - put include in actual query
var query = c in db.standardcodetabletexts.include("standardcodetables"). c.standardcodetable.reftablename == reftablename orderby c.tablecode select c;
Comments
Post a Comment