Disposing of DataContext

6 posts, 2 answers
  1. TommyFlint
    TommyFlint avatar
    4 posts
    Member since:
    Jul 2010

    Posted 09 Oct 2011 Link to this post

    Hello,

    I'm slightly confused of when to dispose of the datacontext as currently I'm developing an MVC project and in one of the controllers i'm doing the following.

    using(Freedom2BookDbContext _dbContext =  new Freedom2BookDbContext)
    {
            Booking = (From Bookings in _dbContext...

            return View(Booking);
    }

    But i always get iObjectScope has already closed.

    Which i'm guessing is because of the lazy loading and in the View i'm using Model.Booking_ID which then needs the iObjectScope.

    But when do i dispose of the DataContext because i don't i randomly get iObjectScope has already closed throughout my application.

    Cheers,

    Tom
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 10 Oct 2011 Link to this post

    Hello Tommyflint,

    You could use Ninject in order to apply dependency injection to your application. Ninject will automatically call the Dispose method of objects implementing the IDisposable interface. Helpful information how to use Ninject can be found here and here.
    Hope that helps.

    Best wishes,
    Damyan Bogoev
    the Telerik team

    Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

  3. DevCraft banner
  4. TommyFlint
    TommyFlint avatar
    4 posts
    Member since:
    Jul 2010

    Posted 10 Oct 2011 Link to this post

    Thanks Damyan Bogoev i'm actually going to try using .ToList() on the iQueryable results and then return it to my view hopefully that should get over the lazy loading hurdle then I can just use the using statement like in my original post.

    Thanks,

    Tom
  5. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 20 Oct 2011 Link to this post

    Hi Tom and Damyan,

    The problem here is that you don't have have the DbContext alive (the same context) during the request... That is when the request hits the server, create a Context, during the request use that Context and dispose the Context when the request finishes.

    Using a construct like using (.......) will dispose the "used" object when the using statement is out of scope... in your case that is after the }

    There are several ways to do this. Usually I use the ContextFactory way of doing it, so when a request comes in I create the Context stores it in HttpContext.Items under a request unique key and when I use it during the request I pull the Context from the HttpContext.Items with that key.

    Check the knowledgebase on this.

    Regards

    Henrik
  6. Answer
    Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 20 Oct 2011 Link to this post

    Hello Henrik,

    Thank you for the valuable information. 
    @Tom: The KB article can be found here. Actually the approach suggested in this article is implemented as a sample application and it is included in the Product SDK.

    Greetings,
    Damyan Bogoev
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  7. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 08 Nov 2011 Link to this post

    Hi Tom,

    Did this answer actually help you?
    If so, please mark the solution as the answer for the thread so that other visitors of the forums can find their answers easier.
    Thanks

    /Henrik
Back to Top
DevCraft banner