c# - Object out of context -
my object eventtypelist goes out of context, if in using. advice?
error message: objectcontext instance has been disposed , can no longer used operations require connection.
public actionresult geteventtypelist() { list<eventtype> eventtypelist; using (var db = new icttbentities()) { eventtypelist = (from et in db.eventtypes select et).tolist(); var result = new { result = "ok", records = eventtypelist }; return json(result, jsonrequestbehavior.allowget); } }
the problem 1 or more relations being lazily loaded rather eagerly loaded. news don't need using
statement dbcontext
context manages connections, leaving little need dispose (cf, http://stephenwalther.com/archive/2008/08/20/asp-net-mvc-tip-34-dispose-of-your-datacontext-or-don-t.aspx). alternatively, can make sure relations have eagerly loaded setting load options context.
note: if use ioc , inject context rather create directly, can avoid problem entirely di framework take care of cleaning context you.
Comments
Post a Comment