ObjectContext instance has been disposed while binding

.net c# entity-framework exception-handling

Question

This is my code:

public class JobsRepository:BaseNewsletterRepository
{
   public IEnumerable<Job> GetJobs()
   {
        var jobs = Newsletterctx.Jobs.Include("Info").OrderBy(o => o.JobID);             
        return jobs.AsEnumerable();
   }
}

public class BusinessNewsletter
{
   public static IEnumerable<Job> GetJobs()
   {      
        using (JobsRepository jobsRepository = new JobsRepository())
        {
            return  jobsRepository.GetJobs();
        }    
    }
}    

rptJobs.DataSource = BusinessNewsletter.GetJobs();
rptJobs.DataBind();

When I try to bind I get error:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Is the only solution, to cast and return List in BusinessNewsletter class?

public static IList<Job> GetJobs()
{
    IList<Job> job;
    using (JobsRepository jobsRepository = new JobsRepository())
    {
        job = jobsRepository.GetJobs().ToList();
    }
    return job;
}

Another related question:

How can I retrieve and bind data to a repeater without specifying a specific type (List<type> or IEnumerable<type> )
I need it in case where I return specific object like this:

var specificJob = from job in Newsletterctx.Jobs.Include("Info")
                  select new
                  {
                      ID = job.JobID,
                      EmailId = job.Info.PreparedEmailID
                  };

I must create specific class just for that object?

1
4
12/18/2011 2:35:15 PM

Accepted Answer

By default Entity Framework uses lazy-loading for navigation properties. That's why these properties should be marked as virtual - EF creates proxy class for your entity and overrides navigation properties to allow lazy-loading. E.g. if you have this entity:

public class MemberLoan
{
   public string LoandProviderCode { get; set; }
   public virtual Membership Membership { get; set; }
}

Entity Framework will return proxy inherited from this entity and provide DbContext instance to this proxy in order to allow lazy loading of membership later:

public class MemberLoanProxy : MemberLoan
{
    private CosisEntities db;
    private int membershipId;
    private Membership membership;

    public override Membership Membership 
    { 
       get 
       {
          if (membership == null)
              membership = db.Memberships.Find(membershipId);
          return membership;
       }
       set { membership = value; }
    }
}

So, entity has instance of DbContext which was used for loading entity. That's your problem. You have using block around CosisEntities usage. Which disposes context before entities are returned. When some code later tries to use lazy-loaded navigation property, it fails, because context is disposed at that moment.

To fix this behavior you can use eager loading of navigation properties which you will need later:

IQueryable<MemberLoan> query = db.MemberLoans.Include(m => m.Membership);

That will pre-load all memberships and lazy-loading will not be used. For details see Loading Related Entities article on MSDN.

165
8/23/2013 8:54:32 AM

Popular Answer

The CosisEntities class is your DbContext. When you create a context in a using block, you're defining the boundaries for your data-oriented operation.

In your code, you're trying to emit the result of a query from a method and then end the context within the method. The operation you pass the result to then tries to access the entities in order to populate the grid view. Somewhere in the process of binding to the grid, a lazy-loaded property is being accessed and Entity Framework is trying to perform a lookup to obtain the values. It fails, because the associated context has already ended.

You have two problems:

  1. You're lazy-loading entities when you bind to the grid. This means that you're doing lots of separate query operations to SQL Server, which are going to slow everything down. You can fix this issue by either making the related properties eager-loaded by default, or asking Entity Framework to include them in the results of this query by using the Include extension method.

  2. You're ending your context prematurely: a DbContext should be available throughout the unit of work being performed, only disposing it when you're done with the work at hand. In the case of ASP.NET, a unit of work is typically the HTTP request being handled.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow