I have a partial class which returns a derived property:
public partial class Consultation
{
public string Name
{
get
{
string n = string.Empty;
n += employee.FirstName;
n += " " + employee.LastName;
return n;
}
}
}
In a business logic layer function I return a List of these entities:
using (var Context = new MMEntities())
{
var cons = Context.Consultations;
return cons.ToList();
}
In a .aspx page I have a databound control which uses the derived property of the entity:
DataTextField="Name"
However, at compile time I get:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I understand my ObjectContext has been disposed because I’ve wrapped it in a ‘Using’ statement.
My question is: How do I include (eager load) the derived Name property in the return method? .Include method only works on navigational properties and I don’t want to have long lived ObjectContexts.
Many thanks…
You don’t eager the
Nameproperty but you must eager load its dependent navigation properties to make it work. So ifemployeein your example is navigation property you must eager load it otherwise yourNameproperty will be dependent on lazy loading and living context.