asp.net - ObjectDisposedException While using Include - why? -
my page calls services layer method uses generic repository "find" method. in services layer method, following:
using (iunitofwork unitofwork = new dbcontext()) { genericrepository<operator> operatorrepos = new genericrepository<operator>(unitofwork); { try { var oper = operatorrepos.find(o => o.operatorid == operatorid).include(o => o.cmn_address).single(); return oper; } catch (invalidoperationexception exc) { //handle exception } } }
the find method repository:
public iqueryable<t> find(func<t, bool> predicate) { return _objectset.where<t>(predicate).asqueryable(); }
on page, try access cmn_address navigation property of operator , following error:
the objectcontext instance has been disposed , can no longer used operations require connection.
i realize caused using statement dispose of context, thought include method eager load cmn_address object. don't understand why doesn't work expected.
you using func<>
instead of expression<func<>>
in condition. makes linq-to-objects. change permanent. calling asqueryable
doesn't make linq-to-entities again.
Comments
Post a Comment