2

I would like to ask about "using" statement in Entity Framework. As I saw several time in other forums and books, that it's a good practice when you are quering database using following code (from ASP.NET MVC4 Appliocation):

using (var db = new ProductsEntites())
{
   var result = db.Products.ToList();
   return View(result);
}

But in other hand, if we are using scaffolding to generate contorller methods and view, default generator is declaring

private ProductsEntites db = new ProductsEntites()

at controller level, so in this case memory that are used to store query results are released only when timeout usage expired and garbage collector unlock memory for other needs. So what is better for small web site and what is best for big

4 Answers 4

4

You can override Dispose method of the controller. It should be called in the end of request.

    protected override void Dispose(bool disposing) {
        if(disposing)
            db.Dispose();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

it's already here, by default following code is generated: protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); }
2

having

private ProductsEntites db = new ProductsEntites()

as a class variable and Dispose it in the controllers Dispose Method seems fine to me. Like Mehmet Ataş pointed out:

protected override void Dispose(bool disposing) {
    if(disposing)
        db.Dispose();
}

The controller is disposed after the execution of an action.

2 Comments

That's what I've missed/don't understand before: "The controller is disposed after the execution of an action." Thank's for clarification
Why do we need to dispose it although we are are using the "Using" statement? I don't understand it I always thought "Using" takes care of that himself. Thanks for the hint solved a issue for me.
1

The Entity Framework was developer to works fine even you not call the Dispose method (when you using a using statement you are implicity calling Dispose method).

It works fine because the EF use a Design Pattern call Fly Weight. In essence a piece of data always remains in memory. It's because that the EF has a delay in first execution of server, and when you kill de aplication has a delay to.

So you can use the code like scaffolding template without worrying about.

1 Comment

I'm just concerning about memory usage, if 100000 users will come :)
0

be wary of the using statement with EF, ensure that all your IQueryable returns are materialised by converting them into an IEnumerable by using ToList or ToArray etc.

If you don't, lazy loading could try and access the context to grab some navigation properties as they are used, if the context is disposed, you will get an exception thrown.

2 Comments

This is not a problem with the using statement, this is a general problem with EF. The same problem could occur even without the using statement, as the context could be disposed at any time after the controller is disposed. Any reference to the IQueryable could throw the same exception. It's true that this is unlikely to happen in the space of a single request, but it's possible. The using statement just makes it happen faster.
I'm always calling .ToList() at and as recommended by Julie Lermann and other gurus. I'm just interested which method you are using - default that are generated by scaffolding or you explicitly calling "using" statement in each Action?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.