This is a migrated thread and some comments may be shown as answers.

Disposing of DataContext

5 Answers 130 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
TommyFlint
Top achievements
Rank 1
TommyFlint asked on 09 Oct 2011, 12:21 PM
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

5 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 10 Oct 2011, 03:44 PM
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.

0
TommyFlint
Top achievements
Rank 1
answered on 10 Oct 2011, 03:53 PM
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
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 20 Oct 2011, 06:58 AM
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
0
Accepted
Damyan Bogoev
Telerik team
answered on 20 Oct 2011, 05:19 PM
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!

0
IT-Als
Top achievements
Rank 1
answered on 08 Nov 2011, 12:45 PM
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
Tags
Development (API, general questions)
Asked by
TommyFlint
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
TommyFlint
Top achievements
Rank 1
IT-Als
Top achievements
Rank 1
Share this question
or