8

I'm looking for a best practices kind of answer here.

Given that best practices for interacting with classes that implement IDisposable is via the Using statement - What is the best practice for using EF lazy loading with MVC?

Example controller method:

<HttpGet>
Public Function Schedule(ByVal id As Int64) As ActionResult

    Dim model As Schedule = Nothing
    Using database As dataContext = New dataContext
        model = (From s In database.Schedules Where s.ScheduleID = id Select s).FirstOrDefault
    End Using

    Return View(theSchedule)

End Function

This example makes lazy loading not work because the database [dataContext] is disposed by time the model arrives in the View.

So I guess the question is:
What are best practices for using lazy loading in MVC? How do you guarantee that your database context is disposed of properly and that you don't cause memory leaks?

1 Answer 1

9

In general, you don't need to use Using statements with Entity Framework data contexts. Lazy collections is one of the reasons why. So your code would simply be:

<HttpGet>
Public Function Schedule(ByVal id As Int64) As ActionResult

    Dim model As Schedule = Nothing
    Dim database As dataContext = New dataContext
    model = (From s In database.Schedules Where s.ScheduleID = id Select s).FirstOrDefault

    Return View(model)

End Function

Data contexts in Entity Framework are designed to open and close connections as needed, and they dispose of themselves automatically when the data context object is no longer required.

The default behavior of DbContext is that the underlying connection is automatically opened any time is needed and closed when it is no longer needed. E.g. when you execute a query and iterate over query results using “foreach”, the call to IEnumerable.GetEnumerator() will cause the connection to be opened, and when later there are no more results available, “foreach” will take care of calling Dispose on the enumerator, which will close the connection.

The only time you would have to be careful about IDisposable is if you Override the data context's default behaviors.

Further Reading

Do I always have to call Dispose on DBContext? Nope.
Managing DbContext the right way with Entity Framework 6: an in-depth guide

5
  • I'm not sure that the second half of you fourth sentence is correct: "and they dispose of themselves automatically when the data context object goes out of scope". Since the data context falls out of scope by time its in the View, using your example the context would still be available, and thus not have disposed itself? Am I missing something?
    – Sam Axe
    Feb 19, 2015 at 3:49
  • Nope. Your right; I was a bit sloppy in my wording. The correct description is that the DBContext disposes of itself when it's no longer needed; i.e. the lazy collection gets fully enumerated or abandoned. See my updates. Feb 19, 2015 at 3:55
  • Thank you much for taking the time to answer this, Robert. It is much appreciated.
    – Sam Axe
    Feb 19, 2015 at 3:57
  • 1
    Note that having a DBContext that is well-behaved without a using statement makes it much nicer to dependency-inject the DBContext, if desired. Feb 19, 2015 at 3:58
  • DI is something I'll be looking into in the near future - so I'll keep that in mind.
    – Sam Axe
    Feb 19, 2015 at 4:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.