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?